0

I am using Rails 5.2.1 and ruby 2.5.0 for the development of my new project. I need to permit user params which has the following structure

{
   "user_id": 1
   "name": "John",
   "pets": [
              {
                "id": 1,
                "count": 5
              },
              {
                "id": 2,
                "count": 3
              },
            ]
}

My User model has following lines

  has_many :pets, dependent: :destroy
  accepts_nested_attributes_for :pets

and in the controller

 params.require(:user).permit(:user_id, :name, pets_attributes: %i(id, count))

But when I post the above json request it produces the following error

Unpermitted parameter: :pets

I couldn't find any solution. Please help. Thanks

CR7
  • 680
  • 2
  • 8
  • 24
  • Possible duplicate of [Nested attributes unpermitted parameters](https://stackoverflow.com/questions/15919761/nested-attributes-unpermitted-parameters) – Gagan Gupta Aug 22 '18 at 09:34

1 Answers1

0

Firstly, are the params being sent in this format?: {user: {user_id: ...}}

Irrespective of the format sent, one issue with your code is pets_attributes should be as follows:

pets_attributes: %i(id count) or pets_attributes: [:id, :count]

So

params.require(:user).permit(:user_id, :name, pets_attributes: %i(id count))

OR

params.require(:user).permit(:user_id, :name, pets_attributes:[:id, :count])

Hope this helped.

Ravi Teja Dandu
  • 466
  • 2
  • 7