I have the following options and can't make strong options work.
Here is my basic code executed in the Rails console for simplicity:
json = {
id: 1,
answers_attributes: {
c1: { id: "", content: "Hi" },
c2: { id: "", content: "Ho" }
}
}
params = ActionController::Parameters.new(json)
All I read says the following should work, but it only gives me idan empty hash answers_attributes:
params.permit(:id, answers_attributes: [:id, :content])
=> { "id"=>1, "answers_attributes"=>{} }
If I instead manually list c1and c2(for example, below), this works, but it is really stupid, because I do not know how many answers the user will supply, and this is a lot of duplication:
params.permit(:id, answers_attributes: { c1: [:id, :content], c2: [:id, :content] })
=> {"id"=>1, "answers_attributes"=>{"c1"=>{"id"=>"", "content"=>"Hi"}, "c2"=>{"id"=>"", "content"=>"Ho"}}}
I tried to replace c1, and c2on 0and 1, but I still have to manually specify 0and 1my permission instructions.
How to resolve an array of unknown lengths of nested attributes?