How to find attribute when getting ActiveModel :: ForbiddenAttributesError

When using strong_params and getting an ActiveModel::ForbiddenAttributesError exception, how do I know which attribute is prohibited? I just switched from attr_accessible , and the debug message was usually pretty good, but not when switching to strong options.

I get this error:

 ActiveModel::ForbiddenAttributesError in SnippetsController#create 

This is a nested model.

 def snip_params params.require(:snippet).permit(:content, :approved, :user_id, :book_id) end 

In parent I used

 has_nested_attributes :snippets 

Create

 def create @snippet = @book.snippets.create(snip_params) @snippet.user = current_user if @snippet.save redirect_to @book flash[:success] = "Snippet submitted and awaiting approval." else flash[:base] = "Someone else has submitted a snippet, please try again later" redirect_to @book end end 

Params Content:

 {"utf8"=>"βœ“", "authenticity_token"=>"bTRSwFRIhN3l3DkkWPtLzpoQHYD+CezmJQLw8Oz5+3g=", "snippet"=>{"content"=>"<p>AAAAAAAAAAAAA</p>\r\n"}, "commit"=>"Create Snippet", "book_id"=>"1"} 
+6
source share
2 answers

All attributes are initially prohibited. This exception only occurs when you do not allow any attributes. If you allow some, and not others, then the log output tells you which options were not allowed.

  params = ActionController::Parameters.new(name: 'Bob', age: 24) #params are usually set automatically in the controller Person.new(params) 

The above will throw an exception

  Person.new(params.permit(:name)) 

This will create a person named "Bob", the log output will also contain:

 Unpermitted parameters: age 
+2
source

Source: https://habr.com/ru/post/959046/


All Articles