简体   繁体   English

如何使用image_tag作为复选框(带有simple_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. 这个实例是图像,因此我不想显示id而是显示它们的图像。 I'm using paperclip, so the basic usage is image_tag model.image.url 我正在使用回形针,因此基本用法是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 ): 这是我的代码(根据此源: 使用Rails 4和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 : 即使指定了image_tag我仍然有这个输出: 在此处输入图片说明

and this is more or less what I'd like : 这或多或少是我想要的:

在此处输入图片说明

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. 我也用它(i.url)代替了文字("image.url")字符串,仅使用普通的旧image_tag就可以了,而无需将其包装在字符串中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM