Deeply nested strong parameters in Rails

38 views Asked by At

I have a jsonb column that stores json in my database and I am trying to use strong parameters to allow certain fields but cannot get it working. When I do the following, I see all parameters as expected (duh!):

def daily_checkin_question_params
  params.require(:daily_checkin_question).permit!
end

But when I attempt to add the parameters I run into an issue. The data is structured like this:

{
  question_type: 'single-select',
  name: 'Which describes your mood?',
  metadata: {
    question_data: {
      unit: 'mood',
      options: [
        { label: 'Happy', value: 'happy' },
        { label: 'Sad', value: 'sad' },
      ]
    }
  }
}
         

All fields work as expected other than the value param within the options array. For some reason that is converted to nil. When permitting all params (like above) this issue does not arise so I know it has to do with my strong parameters.

def daily_checkin_question_params
  params.require(:daily_checkin_question).permit(
    :question_type, :name, :position, :employee_id,
    metadata: [
      validation: [
        min: validation_params,
        max: validation_params,
        required: validation_params
      ],
      question_data: [
        :min, :max, :step, :unit, :total_stars, :allow_half_star,
        :multiline, options: [:label, :value]
      ]
    ]
  )
end
0

There are 0 answers