Home > Blockchain >  What is the Strong Parameter for updating nested record?
What is the Strong Parameter for updating nested record?

Time:11-08

I'm a junior backend developer starting with Rails 7.0.

Looking at the documentation of Rails here:

https://guides.rubyonrails.org/form_helpers.html#the-fields-for-helper-index-option

point 8.3 , it is stated that we can use :index option to output the form. This is the example taken from the doc

{
  "person" => {
    "name" => "Bob",
    "address" => {
      "23" => {
        "city" => "Paris"
      },
      "45" => {
        "city" => "London"
      }
    }
  }
}

I am able to output the form correctly in views, but unable to create the correct strong parameters for that structure.

this is my current strong parameter:

    params.require(:person)
          .permit(:name, address: [:id, :city])

this produce error unknown attribute address, so I'm unable to put the record on database

The doc doesn't give any explanation on how to do strong parameter on nested attribute with index.

Thank you

CodePudding user response:

Your model should have

accepts_nested_attributes_for :address

for nested attributes to work. Or, the attribute address is misspelled in your model.

  • Related