ActiveModel::ForbiddenAttributesError

This one was really frustrating. It has to do with how the CanCan gem works together with the strong parameter feature that is new in Rails 4.

I was getting: ActiveModel::ForbiddenAttributesError whenever I created a new record. Following the instructions here, I fixed the issue by placing the following code before the load_and_authorize_resource method in my controllers/application_controller.rb file:

  before_filter do  # fix ActiveModel::ForbiddenAttributesError
    resource = controller_name.singularize.to_sym
    method = "#{resource}_params"
    params[resource] &&= send(method) if respond_to?(method, true)
  end
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s