【问题标题】:Allowing has_many multiselect with rails 4 & active admin允许使用 rails 4 和活动管理员进行 has_many 多选
【发布时间】:2014-05-13 08:38:23
【问题描述】:

在使用带有 rails 4 的 activeadmin 时,必须设置 permit_params 以允许保存字段。

简单字段可以正常工作,但是 has_many 字段的多选会被忽略。如何为该字段设置 permit_params?

所以家里有_many 供应商,我的管理员看起来像这样:

ActiveAdmin.register Home do
  permit_params :title, :intro, :providers, :providers_attributes => [:id]
  menu :parent => "Content" , :label => "Home Page"
   form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs do
      f.input :title
      f.input :intro
      f.input :providers
    end
    f.actions
  end

  index do
    column :link
    actions
  end
end

【问题讨论】:

    标签: ruby-on-rails-4 activeadmin


    【解决方案1】:

    我不知道他们是否用 Rails 4 改变了它.. 但我一直都是这样做的

    form do |f|
        f.semantic_errors *f.object.errors.keys
        f.inputs do
            f.input :title
            f.input :intro
        end
        f.has_many :providers do |pf|
            pf.input :title #or whatever attributes you have there
            pf.input :_destroy, :as => :boolean, :label => "Delete" if !pf.object.nil?
        end
        f.actions
    end
    

    在你的 model.rb 中你应该有类似的东西

    attr_accessible :providers_attributes
    
    has_many :providers
    accepts_nested_attributes_for :providers, :allow_destroy => true
    

    如果有帮助,请告诉我!

    【讨论】:

    • 谢谢,但是 Rails 4 改变了一切。强参数意味着我们现在需要在控制器或活动管理类中设置允许的属性。另外,我不希望使用嵌套表单来创建新的提供者,我只想能够选择其中的一些 - 它用于主页上的特色框
    猜你喜欢
    • 2013-08-10
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多