我收到JSON对象和嵌套阵列经由轨道4的API使用参数,如下所示:更新阵列具有很强的参数轨道4
{
"token" => "123"
"lessons" => [
{
"token_id" => "j12l3n123",
"attr_1" => "hello",
"attr_2" = "is it me you're looking for"
},
{
"token_id" => "j12l",
"attr_1" => "Nope",
"attr_3" = "You're not."
}
]
}
和我有一个控制器,像这样:
def update_all
#fetch collection with one db hit
token_ids = params[:lessons].map{|l| l[:token_id]}
@lessons = Lesson.where(token_id: token_ids)
params[:lessons].each do |l|
lesson = @lessons.detect { |lesson| lesson.token_id == l[:token_id] }
# How do I update the record with strong params?
lesson.update_attributes(lesson_params)
end
end
private
def lesson_params
params.permit(
:attr_1,
:attr_2,
:attr_3
)
end
我如何使用数组中的正确对象更新每条记录,并使用强参数来执行此操作?
如何在控制器中使用它? –
没有为我工作,感谢您花时间回答。无论如何,我已经找到了解决方案,并发布了答案。 –