Wonderful ActiveModel :: ForbiddenAttributesError

I am using strong_params and trying to pass an object. I have two questions.

  • How do I know which attribute is causing the problem?
  • What am I missing in the code below?

Let's start with the error, the magazine is not telling me anything.

ActiveModel::ForbiddenAttributesError in JobsController#create 

Just for a giggle, here is a magazine that I don't see very useful:

 Started POST "/jobs" for 127.0.0.1 at 2013-12-17 22:03:59 +0000 Processing by JobsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ohq4lVqPVMpjzbZwCfJNYBL78TAcoC0WZLSmpCzMD3k=", "job"=>{"job_title"=>"Junior Rails Developer", "job_description"=>"A new position getig nsomethfins lansnana", "languages"=>["", "Rails", "Ruby"], "country_code"=>"AO", "job_type"=>"Full-Time", "job_salary"=>"30000", "job_level"=>"Junior"}, "commit"=>"Create Job"} User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1 Completed 500 Internal Server Error in 8ms ActiveModel::ForbiddenAttributesError (ActiveModel::ForbiddenAttributesError): 

It makes sense, but then if I look at my creation:

 def create binding.pry @job = Job.new(job_params) respond_to do |format| if @job.save format.html { redirect_to @job, notice: 'Job was successfully created.' } format.json { render action: 'show', status: :created, location: @job } else format.html { render action: 'new' } format.json { render json: @job.errors, status: :unprocessable_entity } end end end 

Strong_params:

  def job_params params.require(:job).permit(:job_title, :job_level, :job_description, :job_salary, :country_code, :job_type, :state, :languages => []) end 

I'm mostly interested in learning how to find out where the problem is for the future, as it looks like a needle in a hay bug.

+7
ruby-on-rails
source share
3 answers

Your log file displays the parameters that the application receives from create:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ohq4lVqPVMpjzbZwCfJNYBL78TAcoC0WZLSmpCzMD3k=", "job"=>{"job_title"=>"Junior Rails Developer", "job_description"=>"A new position getig nsomethfins lansnana", "languages"=>["", "Rails", "Ruby"], "country_code"=>"AO", "job_type"=>"Full-Time", "job_salary"=>"30000", "job_level"=>"Junior"}, "commit"=>"Create Job"} 

You need to first make sure that each parameter is specified in the definition of strong parameters. Then make sure that each parameter is correct.

 :parameter #accepts a single entry :parameter => [] #accepts a hash :paramters_attributes: [:firstattribute, :secondattribute] #accepts nested parameters 

Your languages ​​option displays as a hash in your log, but you only have languages ​​in your allowed options. Changing the parameter for accepting hashes should solve the problem.

 :languages => [] 

Also this blog post is useful, showing some powerful parameters on an example: http://blog.sensible.io/2013/08/17/strong-parameters-by-example.html

+2
source share

This probably doesn't apply in this case, but I had a problem with Rails 4, but only with the create action. Looking through the full stack trace in the development log, some links to CanCan and a model of features that I used for access control were shown.

I started using load_and_authorize_resource as recommended at the top of the controller, but when I deleted it and just resolved each long arm action, the parameter problem disappeared.

+1
source share

This error occurs when trying to mass assign a hash parameter containing non-disconnected keys. Your job_params method returns a hash containing only the key / values ​​specified in the permit call, so that everything is in order ...

Are you sure the error occurred in the hosted code segment? The only possible location is the line @job = Job.new(job_params) , is this line mentioned in the return line? If not, could you post the full answer? Since an error should happen in a filter or observer or something like that ...

0
source share

All Articles