如何使用image_tag作为复选框(使用simple_form和paperclip)?(How to use image_tag as checkboxes (with simple_form and paperclip)?)

我想显示一个带复选框的实例集合。 这个实例是图像所以不是显示id我想显示他们的图像。 我正在使用image_tag model.image.url ,所以基本用法是image_tag model.image.url

这是我的代码(根据此来源: 使用Rails 4和简单表单将图像标记添加到复选框输入字段 ):

= 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

即使指定了image_tag我仍然有这个输出:

这或多或少是我想要的:

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 :

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

最满意答案

使用地图,而不是每个。

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

我也做了它(i.url)而不是文字("image.url")字符串,只使用普通的旧image_tag应该工作,不需要将它包装在一个字符串中。

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.

更多推荐