Skip to content

Showing an uploaded image in the form

houen edited this page Jan 26, 2012 · 2 revisions

To show a preview of a form image, use the :hint form parameter, like so (thank to Greg Tangey for this on the mailing list:


f.inputs "Attachment", :multipart => true do 
  f.input :cover_page, :as => :file, :hint => f.template.image_tag(f.object.image.url) 
  f.input :cover_page_cache, :as => :hidden 
end

Of course you can still do all the processing you are used to from PaperClip or CarrierWave:


f.inputs "Attachment", :multipart => true do 
  f.input :cover_page, :as => :file, :hint => f.template.image_tag(f.object.image.url(:thumb)) 
  f.input :cover_page_cache, :as => :hidden 
end

Remember the _cache input field to get previews in non-saved records with e.g. CarrierWave. See Ryans Railscast on CarrierWave for more information