【问题标题】:Rails ActiveAdmin display image in has_many relationshipRails ActiveAdmin 在 has_many 关系中显示图像
【发布时间】:2020-07-23 21:46:42
【问题描述】:

我创建了一个包含多个画廊的作品。

class Production < ApplicationRecord
  has_many :galleries
  accepts_nested_attributes_for :galleries, :allow_destroy => true
  ...

一个画廊有_one_attached照片

class Gallery < ApplicationRecord
    has_one_attached :photo
end

在我的 ActiveAdmin 表单中,我添加了一个 has_many 字段和一个要显示的条件图像。

...
f.has_many :galleries, allow_destroy: true do |g|
  g.input :photo, as: :file
  g.input :caption
  if g.object.photo.attached?
    image_tag(g.object.photo.variant(resize: '200x200^'))
  end
end
...

图像显示,问题是图像显示与相关字段分开。图库中所有上传的图像都显示在整个字段上方 - 就在字段标签上方。有什么方法可以将相关字段和示例图像一起保存在标记中?任何帮助将不胜感激,谢谢!

【问题讨论】:

    标签: ruby-on-rails activeadmin rails-api


    【解决方案1】:

    我使用:hint 选项解决了这个问题,例如

    g.input :photo, as: :file, hint: image_tag(g.object.photo.variant(resize: '200x200^'))

    【讨论】:

      【解决方案2】:

      尝试用一些 arbre dsl 包围条件:

      li(:class => 'input') do
        label(:class => 'label') { "Preview" }
        div(:class => 'myCustomClass') do
          if g.object.photo.attached?
             # your code
          end
        end
      end
      

      rails 视图助手(如 image_tag)并不总是能很好地与 arbre 的输出缓冲配合使用。

      【讨论】:

        猜你喜欢
        • 2014-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多