How to use Rails 4 strong parameters with has_many :through association?

后端 未结 3 1109
遇见更好的自我
遇见更好的自我 2020-12-08 00:46

I\'m having trouble getting a has_many :through association working with Rails 4\'s strong parameters. I have a model called Checkout and I need to select a per

3条回答
  •  被撕碎了的回忆
    2020-12-08 01:20

    Keep in mind that the name you give to your strong parameters (employees, employee_ids, etc.) is largely irrelevant because it depends on the name you choose to submit. Strong parameters work no "magic" based upon naming conventions.

    The reason https://gist.github.com/leemcalilly/a71981da605187d46d96 is throwing an "Unpermitted parameter" error on 'employee_ids' is because it is expecting an array of scalar values, per https://github.com/rails/strong_parameters#nested-parameters, not just a scalar value.

    # If instead of:
    ... "employee_ids" => "1" ...
    # You had:
    ... "employee_ids" => ["1"]
    

    Then your strong parameters would work, specifically:

    ... { :employee_ids => [] } ...
    

    Because it is receiving an array of scalar values instead of just a scalar value.

提交回复
热议问题