12

I using rails 4.0.3 and am trying to set up many to many checkboxes in Active-Admin. The checkbox selections are not being saved. This is what i have

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

I have also tried using has_and_belongs_to_many but still cant not get the selections to save.

Any guidance would be highly appreciated.

Cheers

user346443
  • 4,672
  • 15
  • 57
  • 80

2 Answers2

23

I found that adding the following to your active_admin file, product.rb, fixes it.

ActiveAdmin.register Product do
  permit_params category_ids: []
end
jburris
  • 231
  • 2
  • 3
  • Still gold answer! Wohoo – fruqi May 09 '16 at 14:19
  • To be specific, this is something that must be done for strong parameters. The list passed to `permit_params` is normally only a list of "simple" parameters, but if you want to receive an array, you must add the parameter as shown in this answer. https://stackoverflow.com/a/16555975/1589422 – Robin Daugherty May 18 '18 at 18:31
5

try add

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

Djizeus
  • 4,161
  • 1
  • 24
  • 42
stopanko
  • 306
  • 3
  • 7