'Why am I getting "Unpermitted parameter: format"?
I'm getting these parameters in my Rails controller:
Parameters: {"id"=>"238", "user_id"=>1, "group_id"=>43, "title"=>"asdasdasd 1111 ultadas ult asdas", "description"=>"asdssss", "state_id"=>nil, "allow_anonymous_answer"=>false, "initial_message"=>"asdasdsadas", "final_message"=>"dasdasdsad", "poll_pages"=>[{"id"=>382, "poll_id"=>238, "title"=>"Pagina 1", "description"=>"Pagina 1", "position"=>1, "created_at"=>"2016-05-06T14:25:56.795Z", "updated_at"=>"2016-05-06T14:25:56.795Z"}], "poll"=>{"id"=>"238", "user_id"=>1, "group_id"=>43, "title"=>"asdasdasd 1111 ultadas ult asdas", "description"=>"asdssss", "state_id"=>nil, "allow_anonymous_answer"=>false, "initial_message"=>"asdasdsadas", "final_message"=>"dasdasdsad"}}
Yet I get this message: Unpermitted parameter: format
Even though I'm using:
params.permit(:id, :user_id, :title, :description, :state_id, :group_id, :allow_anonymous_answer, :initial_message, :final_message, poll: [:id, :user_id, :title, :description, :state_id, :group_id, :allow_anonymous_answer, :initial_message, :final_message], poll_pages: [:id, :poll_id, :title, :description, :position, :created_at, :updated_at])
Solution 1:[1]
you need to send correct format like:
params.require(:MODEL).permit(:id, :user_id, :title, :description, :state_id, :group_id, :allow_anonymous_answer, :initial_message, :final_message, poll_attributes: [:id, :user_id, :title, :description, :state_id, :group_id, :allow_anonymous_answer, :initial_message, :final_message], poll_pages_attributes: [:id, :poll_id, :title, :description, :position, :created_at, :updated_at])
And also _attributes for the nested attributes in your permit params :
poll_attributes: [:id, :user_id, :title, :description, :state_id, :group_id, :allow_anonymous_answer, :initial_message, :final_message]
Solution 2:[2]
You need permitte in strong params the "format", it is in form, but can`t save any value until permitte this.
Solution 3:[3]
This is because when your request is for a specific format, for instance, json (ie: /some_controller/some_method.json), Rails sends a format param:
#<ActionDispatch::Request:0x000000011b5e3898>, params: {"controller"=>"some_controller", "action"=>"some_method", "format"=>"json"} }
There is a global setting that can be set to always permit format params. In Rails 7 looks like:
config.action_controller.always_permitted_parameters = %w( controller action format )
According to the comments in Rails 6, it's like:
config.always_permitted_parameters = %w( controller action format )
Solution 4:[4]
I know this question is old, but I was having a similar problem and found a solution. In addition to what Sunny mentioned, I had to use the "inverse_of" helper method within my two models.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kaushlendra Tomar |
| Solution 2 | Elton Santos |
| Solution 3 | yorch |
| Solution 4 | Clifton Regis |
