Search code examples
ruby-on-railsjsonparametersparameter-passingstrong-parameters

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.


Solution

  • 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,
       ] 
    )