Search code examples
ruby-on-railspaperclipruby-on-rails-5simple-form

How to use image_tag as checkboxes (with simple_form and paperclip)?


I would like to display a collection of instances with checkboxes. This instances are images so instead of displaying there id I would like to display their images. I'm using paperclip, so the basic usage is image_tag model.image.url

This is my code (according to this source: Add Image Tag to Checkbox Input Field with Rails 4 and Simple Form):

 = f.input_field :inspiration_image_ids, 
   :collection => InspirationImage.all.each {|i| "#{image_tag('image.url')}".html_safe }, 
   :include_blank => '(All)', 
   :multiple => true, 
   :selected => [''], 
   as: :check_boxes

Even with specifying the image_tag I still have this output : enter image description here

and this is more or less what I'd like :

enter image description here


Solution

  • Use map, not each.

    InspirationImage.all.map {|i| image_tag(i.url).html_safe },
    

    Also I made it (i.url) instead of the literal ("image.url") string, and just using plain old image_tag should work, no need to wrap it in a string.