Problem with Strong Parameters and Nested Attributes

I’m on Rails 4.0, converting Protected Attributes to Strong Parameters, and I have the following error in some of my tests:

Failure/Error: put :update, @args
     ActionController::UnpermittedParameters:
       found unpermitted parameters: 0, 1

Those values are the keys from the nested attributes hash. That is, “params" looks like:

{"group"=>{"name"=>"Changed Name", "book_groups_attributes"=>{0=>{"book_id"=>"46512", "id"=>"", "_destroy"=>"0"}, 1=>{"book_id"=>"46513", "id"=>"5272", "_destroy"=>"1"}}}, "commit"=>"Save Changes", "id"=>"4564", "controller"=>"groups", "action"=>"update”}

Here is the require/permit statement:

params.require(:group).permit(:name, :_destroy, book_groups_attributes: %i(id _destroy book_id))

Any idea what I’m doing wrong?

I figured it out for myself. The param list did not “exactly” match what I have in production. Specifically, the nested has keys (0, 1, …) should be quite strings (“0”, “1”, … ) instead of numbers. With that change, the examples passed as they did prior to changing over to Strong Parameters.

···

On Sep 27, 2020, at 1:10 PM, Jack Royal-Gordon <jackrg@pobox.com> wrote:

I’m on Rails 4.0, converting Protected Attributes to Strong Parameters, and I have the following error in some of my tests:

Failure/Error: put :update, @args
     ActionController::UnpermittedParameters:
       found unpermitted parameters: 0, 1

Those values are the keys from the nested attributes hash. That is, “params" looks like:

{"group"=>{"name"=>"Changed Name", "book_groups_attributes"=>{0=>{"book_id"=>"46512", "id"=>"", "_destroy"=>"0"}, 1=>{"book_id"=>"46513", "id"=>"5272", "_destroy"=>"1"}}}, "commit"=>"Save Changes", "id"=>"4564", "controller"=>"groups", "action"=>"update”}

Here is the require/permit statement:

params.require(:group).permit(:name, :_destroy, book_groups_attributes: %i(id _destroy book_id))

Any idea what I’m doing wrong?

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;