igorkasyanchuk/active_storage_validations

Known Issue

limitlessmind opened this issue · 1 comments

This is regarding the known issue generating image preview. In my case an image variant. It's written that this code was fixed in Rails 6. I'm using Rails 7.

When adding an unsupported file, like a PDF when it should only accept PNG or JPEG, the preview image errors out because I am using:

<% if user.image.attached? %>

In my case it makes sense because I am using the preview on the same page as the form partial and if the image is invalid, like a PDF in this case, it would not be attached.

You have that we need to use this up to Rails 5:

<% if user.image.attached? && user.image.attachment.blob.present? && user.image.attachment.blob.persisted? %>

Would only using <% if user.image.attachment.blob.persisted? %> be OK? It does work but do you know of any use cases where all three would be required?

The error seems not completely gone away in Rails 7. I used this:

<%= 
  image = 
    if user.image.attached? && user.image.attachment.blob.present? && user.image.attachment.blob.persisted?
      user.image.variant(:thumb)
    elsif resource.reload.picture.attached?
      user.image.variant(:thumb)
    else
      user.image_default(:thumb) # custom method
    end

  image_tag(image, :class => 'mb-3')
%>