Search code examples
ruby-on-railsrubyruby-on-rails-3strong-parameters

ActiveModel::ForbiddenAttributes error


I have done the following things. I installed strong_parameters gem.

1)I created a initializer and added the following line ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)

2)I removed attr_accessible from model.

Then i was tried to create a new record, it's was not going to the create method itself. it was giving ActiveModel::ForbiddenAttributes error. Please explain me what could be the problem?

Please find the code below:

config/initializers/strong_parameters.rb

 ActiveRecord::Base.send(:include, ActiveModel::ForbiddenAttributesProtection)

app/models/role.rb

 class Role < ActiveRecord::Base
  #attr_accessible :name, :description
  validates :name, presence: true, uniqueness: { case_sensitive: false}
 end

app/controllers/roles_controller.rb

 class RolesController < ApplicationController

  def create
   @role = Role.new(role_params)
   if @role.save
     redirect_to roles_path, notice: t('Role was successfully created.', default: 'Role was successfully created.')
  else
     render action: "new"
  end
 end

 private

 def role_params
  params.require(:role).permit(:name,:description)
 end

end

EDIT : Please find the error message:

enter image description here


Solution

  • It's a problem with cancan gem. I tried the below skip_load_resource. It's working now.

     load_and_authorize_resource skip_load_resource only: [:create]