Solving Active Admin Mass-Assignment Issues for Rails 3.2
In my rails project, application.rb has the following line by default:
config.active_record.whitelist_attributes = true
This means that all attributes are protected from mass-assignment unless opened up in the model by doing something like:
attr_accessible :xyz, :abc
Now active admin does mass assgignments all over the place when updating stuff from its admin portal.
add all attributes to attr_accessible, which would be stupid or
we can update each model under the admin directory and list out the things its going to show, update etc.
The 2nd solution somewhat works but the whole point of using active admin is so i can open up the whole thing for my co-founder and he can go nuts, tweaking things. Writing every models accessible fields will drive me nuts which is not cool.
So this is what i ended up doing:
First, to each model under app/models I added the following line:
attr_accessible *column_names, :as => :admin
Then In the active admins initializer file active_admin.rb i added the following:
module ActiveAdmin class BaseController with_role :admin end end
Thats it! Now all columns are accessible as long as the role making the udpate is admin and I have instrcuted active admin's base controller to make all updates using role admin
http://apidock.com/rails/v3.1.0/ActiveRecord/Persistence/update_attributes
https://github.com/josevalim/inherited_resources/pull/153