Rails 4.0.3 Active-Admin has_many复选框不保存

时间:2014-03-17 15:38:45

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

我使用的是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,但仍然无法选择保存。

任何指导都将受到高度赞赏。

干杯

2 个答案:

答案 0 :(得分:21)

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

ActiveAdmin.register Product do
  permit_params category_ids: []
end

答案 1 :(得分:5)

尝试添加

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