Note that there are some explanatory texts on larger screens.

plurals
  1. POPaperclip wrong attachment url on validation errors
    primarykey
    data
    text
    <p>I have a preview of attached image in my update form. The problem appears when user gets validation errors on attachment field. In this case image thumbnail url becomes as if an image was uploaded without any errors (it shows name of file that was not saved at server).</p> <p>Here is how I get image url in my view: <code>&lt;%= image_tag(@product.photo.url(:medium)) %&gt;</code>.</p> <p>Controller:</p> <pre><code>def update @product = Product.find(params[:id]) @product.update_attributes(params[:product]) ? redirect_to('/admin') : render(:new) end def edit @product = Product.find(params[:id]) render :new end </code></pre> <p>Model:</p> <pre><code>class Product &lt; ActiveRecord::Base &lt;...&gt; @@image_sizes = {:big =&gt; '500x500&gt;', :medium =&gt; '200x200&gt;', :thumb=&gt; '100x100&gt;'} has_attached_file :photo, :styles =&gt; @@image_sizes, :whiny =&gt; false validates_attachment_presence :photo validates_attachment_content_type :photo, :content_type =&gt; ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'], :message =&gt; I18n.t(:invalid_image_type) validates_attachment_size :photo, :less_than =&gt; 1.megabytes, :message =&gt; I18n.t(:invalid_image_size, :max =&gt; '1 Mb') after_post_process :save_image_dimensions &lt;...&gt; end </code></pre> <p><strong>UPD:</strong> The simpliest solution is to add <code>@photo_file_url = @product.photo.url(:medium)</code> in controller's update action before <code>@product.update_attributes</code> and <code>&lt;% @photo_file_url ||= @product.photo.url(:medium) %&gt;</code> in the view.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload