iooi
iooi

Reputation: 523

How to permit an array json with Rails?

For json

{
   "version":"1",
   "configs":[
      {
         "title":"Good",
         "body":"Body"
      },
      {
         "title":"Good",
         "body":"Body"
      }
   ]
}

How to permit it with Rails' params.permit in controller?

I tried

  params.permit(
    config_setting: [
      :version,
      configs: [
        :title,
        :body,
      ]
    ],
  )

But seems not right.

Upvotes: 0

Views: 347

Answers (1)

anonymus_rex
anonymus_rex

Reputation: 579

Currently you are permitting a json with the following structure:

{
   "config_setting": [
      {
         "version":"1",
         "configs":[
            {
               "title":"Good",
               "body":"Body"
            },
            {
               "title":"Good",
               "body":"Body"
            }
         ]
      }
   ]
}

Just add config_setting node to the data or adjust your strong params block to:

params.permit(
   :version,
   configs: [
     :title,
     :body,
   ] 
)

Upvotes: 2

Related Questions