简体   繁体   English

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

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

I am using gem 'activeadmin', '~> 1.3' in a rails app. 我在Rails应用程序中使用gem'activeadmin','〜> 1.3'。 I have these relationships between two models : 我在两个模型之间有这些关系:

class Offer < ApplicationRecord
  has_many :products
end
class Product < ApplicationRecord
  belongs_to :offer, required: false
  has_attachments :photos, maximum: 4, dependent: :destroy
end

I can access a product's first photo this way : @product.photos[0] or @product.photos[0].path 我可以这样访问产品的第一张照片: @product.photos[0]@product.photos[0].path

In admin/offer.rb, the checkboxes to select products belonging to the offer are built with : 在admin / offer.rb中,使用以下选项构建了用于选择属于该商品的产品的复选框:

f.input :products, as: :check_boxes

This renders a checkbox list with product names as labels. 这将显示一个复选框列表,其中包含产品名称作为标签。 Now I want to enrich this label with a thumbnail of one product's photos next to it. 现在,我想在标签旁边加上一个产品的照片缩略图。

I failed to iterate over the ":products" when the checkboxes are built. 构建复选框后,我无法遍历“:products”。 I wanted to add an html tag like this : span img(src: "product_image_path") , as I do when I display the product page in activeadmin. 我想添加一个像这样的html标签: span img(src: "product_image_path") ,就像我在activeadmin中显示产品页面时一样。

I checked formtastic documentation and only found this customization option : :collection => Product.pluck(:name, :id) . 我检查了表格文档,仅发现此自定义选项:collection => Product.pluck(:name, :id) This lets me change only the text in label but do not provide me a way to add an image. 这使我只能更改标签中的文本,而不能提供添加图像的方法。

How can I display a thumbnail of one of the product's photos next to product name in activeadmin form ? 如何以activeadmin形式在产品名称旁边显示产品照片之一的缩略图?

As stated in : formtastic documentation , you can customize existing inputs. 如: 格式文档中所述 ,您可以自定义现有输入。 Thus, I created a checkbox_image input in a app/inputs/checkbox_image_input.rb : 因此,我在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

I use cloudinary as an image storage service, you can update img_tag(choice) according to yours. 我使用cloudinary作为图像存储服务,您可以根据自己的意愿更新img_tag(choice)。 Then you can use the new input in admin/model.rb, in form : 然后,您可以使用admin / model.rb中的新输入,格式为:

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

Enventually I styled the new label tag in my active_admin_custom_css.css file, using input_with_thumbnail class. 无疑,我使用input_with_thumbnail类在active_admin_custom_css.css文件中设置了新标签标签的样式。

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

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