I am trying to create multiple "Absence"s by posting:
Parameters: {"absences"=>[{"user_id"=>1, "lesson_id"=>25,
"excused"=>true}, {"user_id"=>2, "lesson_id"=>25, "excused"=>true}]}
However, I am not able to whitelist this format in the controller. I attempted to follow the solution from "How to use strong parameters with an objects array in Rails".
In my case:
def absence_params
params.permit(absences: [:user_id, :lesson_id, :excused])
end
I get
ActiveModel::UnknownAttributeError (unknown attribute 'absences' for Absence.):
Then I tried:
Parameters: {"absence"=>[{"user_id"=>1, "lesson_id"=>25,
"excused"=>true}, {"user_id"=>2, "lesson_id"=>25, "excused"=>true}]}
def absence_params
params.permit(:absence, array: [:user_id, :lesson_id, :excused])
end
and got:
Unpermitted parameters: :absence, :format
---- Resolved ----
- The gem 'cancancan' was not allowing me to create using an array.
- If you have an issue permitting an array in the strong params, try
params.require(:absences).map do |p|
p.permit(:user_id, :lesson_id, :excused)
end