繁体   English   中英

以activeadmin形式在复选框标签旁边添加图像

[英]Add an image next to a checkbox label in activeadmin form

我在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].path

在admin / offer.rb中,使用以下选项构建了用于选择属于该商品的产品的复选框:

f.input :products, as: :check_boxes

这将显示一个复选框列表,其中包含产品名称作为标签。 现在,我想在标签旁边加上一个产品的照片缩略图。

构建复选框后,我无法遍历“:products”。 我想添加一个像这样的html标签: span img(src: "product_image_path") ,就像我在activeadmin中显示产品页面时一样。

我检查了表格文档,仅发现此自定义选项:collection => Product.pluck(:name, :id) 这使我只能更改标签中的文本,而不能提供添加图像的方法。

如何以activeadmin形式在产品名称旁边显示产品照片之一的缩略图?

如: 格式文档中所述 ,您可以自定义现有输入。 因此,我在app / inputs / checkbox_image_input.rb中创建了一个checkbox_image输入:

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

我使用cloudinary作为图像存储服务,您可以根据自己的意愿更新img_tag(choice)。 然后,您可以使用admin / model.rb中的新输入,格式为:

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

无疑,我使用input_with_thumbnail类在active_admin_custom_css.css文件中设置了新标签标签的样式。

暂无
暂无

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

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