【问题标题】:Activeadmin Nested AttributesActiveadmin 嵌套属性
【发布时间】:2016-08-09 06:47:37
【问题描述】:

我有具有has_many belongs_to关系的模型产品和画廊,我正在实现nested_attributes功能,这里的问题是:当我点击添加画廊时,会创建两个内部表单而不是一个,如下图所示:

代码

    form do |f|
    f.inputs "Product" do
        f.input :title, :required => true
        f.input :description
        f.input :price
        f.input :display_image
        f .input :product_detail, :hint => "Upload PDF file"
        f.input :category
    end

    f.inputs 'Product Gallery' do
        f.has_many :galleries, allow_destroy: true, new_record: 'Add Gallery' do |c|
            c.input :image, :hint => c.template.image_tag(c.object.image.url(:thumb)) 
        end
    end
    f.inputs 'Product Specification' do
        f.has_many :specifications, allow_destroy: true, new_record: true do |c|
            c.input :specification_label
            c.input :specification_details
        end
    end
    f.actions
end

我需要这方面的帮助。我无法解决这个问题!!,任何帮助都将是可观的。

【问题讨论】:

  • 我看不到您的代码,但我之前遇到过同样的情况,您应该将生成动态字段的添加字段链接/按钮放在 f.fields_for :galleries 之外
  • 嗨@aldrien.h 感谢您的回复,我没有在代码中的任何地方使用 f.fields_for,我编辑了我的问题并添加了代码!
  • 我认为因为你有两个循环,这就是它生成两次的原因。
  • 您是否在模型中添加了`accept_nested_attributes_for`?
  • 你允许在这个表单中使用什么参数?

标签: ruby-on-rails-4 activeadmin nested-attributes


【解决方案1】:

试试这个。

   f.inputs 'Product Gallery' do
      f.has_many :galleries do |c|
        c.input :image, :hint => c.template.image_tag(c.object.image.url(:thumb)) 
        c.input :_destroy, :as => :boolean
      end
    end

希望这对你有用。

【讨论】:

  • 感谢@hgsongra 的回答,我试过了,但没有运气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多