Rails 3.1 - Protected attributes cannot be assigned (even if they are added to attr_accessible)

I have a nested form categoriesfor storesand it is listed as attr_accessible in the repository model. But still keep getting the following error:

WARNING: Can't mass-assign protected attributes: category_ids

I tried all attr_accessible options in the store model:

attr_accessible :categories
attr_accessible :category
attr_accessible :category_id
attr_accessible :category_ids

None of them work! Both of these models have has_and_belongs_to_many (and have a join table called category_stores).

Any advice would be greatly appreciated (I have been banging my head on the wall for two days).

UPDATE

I implemented a temporary fix (which is pretty redundant and not required if the rails just stick to the above problem). I fixed it by overwriting the method createfor ActiveAdmin and looping to insert the association data:

  controller do
    def update
      @store = store.find(params[:id])
      if @store.update_attributes(params[:store])
        @store.categories.delete_all
        params[:store][:category_ids].each do |category_id|
          @store.categories << Category.find(category_id) unless category_id.blank?
        end
        redirect_to :action => :index
      else
        redirect_to :action => :edit, :notice => "Something f'ed up"
      end
    end

  end
+5
1

, categories_stores, :

class Store

    has_many :categories, :through => :categories_stores

​​ .

, , , / , :

    accepts_nested_attributes_for :categories_stores

.

: http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

+4

All Articles