Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在activeadmin表单中的复选框标签旁边添加图像_Ruby On Rails_Activeadmin_Formtastic - Fatal编程技术网

Ruby on rails 在activeadmin表单中的复选框标签旁边添加图像

Ruby on rails 在activeadmin表单中的复选框标签旁边添加图像,ruby-on-rails,activeadmin,formtastic,Ruby On Rails,Activeadmin,Formtastic,我正在rails应用程序中使用gem'activeadmin','~>1.3'。我在两个模型之间有以下关系: class Offer < ApplicationRecord has_many :products end class Product < ApplicationRecord belongs_to :offer, required: false has_attachments :photos, maximum: 4, dependent: :destroy en

我正在rails应用程序中使用gem'activeadmin','~>1.3'。我在两个模型之间有以下关系:

class Offer < ApplicationRecord
  has_many :products
end
class Product < ApplicationRecord
  belongs_to :offer, required: false
  has_attachments :photos, maximum: 4, dependent: :destroy
end
课程优惠
我可以通过以下方式访问产品的第一张照片:
@product.photos[0]
@product.photos[0]。路径

在admin/offer.rb中,用于选择产品的复选框包含:

f.输入:产品,作为::复选框

这将呈现以产品名称作为标签的复选框列表。现在,我想用一个产品照片的缩略图来丰富这个标签

在构建复选框时,我未能迭代“:products”。我想添加这样的html标记:
span img(src:“product\u image\u path”)
,就像我在activeadmin中显示产品页面一样

我查看了formtastic文档,只找到了这个定制选项:
:collection=>Product.pull(:name,:id)
。这使我只更改标签中的文本,但不提供添加图像的方法

如何在activeadmin表单中在产品名称旁边显示产品照片的缩略图?

如中所述,您可以自定义现有输入。因此,我在app/inputs/checkbox\u image\u input.rb中创建了一个checkbox\u image input:

class CheckboxImageInput < Formtastic::Inputs::CheckBoxesInput
  include CloudinaryHelper

  def choice_html(choice)
    template.content_tag(
    :label,
    img_tag(choice) + checkbox_input(choice) + choice_label(choice),
    label_html_options.merge(:for => choice_input_dom_id(choice), :class => 
    "input_with_thumbnail"))
 end

  def img_tag(choice)
    cl_image_tag(Product.find(choice[1]).photos[0].path, :width=>30, 
    :crop=>"scale")
  end
end
环境上,我在我的active_admin_custom_css.css文件中使用带有缩略图类的input_设置了新标签标签的样式

f.input :products, as: :checkbox_image, :collection => product_selection