【问题标题】:Rails 4.0.3 Active-Admin has_many checkboxes not savingRails 4.0.3 Active-Admin has_many 复选框未保存
【发布时间】:2014-04-22 21:54:51
【问题描述】:

我使用 rails 4.0.3 并尝试在 Active-Admin 中设置多对多的复选框。未保存复选框选择。这就是我所拥有的

class Product < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations
  accepts_nested_attributes_for :categorizations
end

class Category < ActiveRecord::Base
  has_many :categorizations
  has_many :products, :through => :categorizations
  accepts_nested_attributes_for :categorizations
end

class Categorization < ActiveRecord::Base
  belongs_to :category
  belongs_to :product
end

ActiveAdmin.register Product do

  permit_params :title, :price, category_ids:[:id]

  form do |f|
    f.semantic_errors *f.object.errors.keys
    f.inputs "Product" do
      f.input :title
      f.input :price
      f.input :categories, :as => :check_boxes
    end
    f.actions
  end
end

我也尝试过使用 has_and_belongs_to_many 但仍然无法保存选择。

任何指导将不胜感激。

干杯

【问题讨论】:

  • 我想通了。它应该是 permit_params :title, :price, :category_ids => []
  • 您应该将其发布为答案并接受它

标签: checkbox ruby-on-rails-4 activeadmin has-many-through


【解决方案1】:

尝试添加

permit_params :title, :price, category_ids:[:id], categories_attributes: [:id, :your_fields, :_update,:_create]

【讨论】:

  • 请尝试详细说明您的答案,而不仅仅是发布代码。
【解决方案2】:

我发现将以下内容添加到您的 active_admin 文件 product.rb 可以修复它。

ActiveAdmin.register Product do
  permit_params category_ids: []
end

【讨论】:

  • 仍然是黄金答案!哇
  • 具体来说,这是强参数必须要做的事情。传递给permit_params 的列表通常只是“简单”参数的列表,但如果要接收数组,则必须添加参数,如本答案所示。 stackoverflow.com/a/16555975/1589422
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-05
  • 2014-09-19
  • 2016-09-03
  • 1970-01-01
相关资源
最近更新 更多