[英]Rails 4.2 saving has_and_belongs_to_many association Ids
I have the following bit of code that is working fine with Rails 4.1 with protected_attributes gem (I didn't have my code moved to strong_parameters yet) 我有以下一些代码,使用带有protected_attributes gem的Rails 4.1正常工作(我没有将我的代码移到strong_parameters)
class Employee
has_and_belongs_to_many :skills
attr_accessible :skill_ids, ...
end
class Skill
has_and_belongs_to_many :employees
end
I bind the skills to the employee while updating an employee so my view looks like below 我在更新员工时将技能绑定到员工,因此我的视图如下所示
<%= form_for @employee, do |f| %>
.....
<%= f.collection_select :skill_ids, Skill.all, :id, :name, {},
{:multiple => true, class: 'select2 '} %>
......
<% end %>
skill_ids were part of attr_accessible params so it worked perfectly while saving the employee form. skill_ids是attr_accessible params的一部分,因此它在保存员工表单时工作得很好。 (Note: this doesn't even require accepts_nested_attributes_for :skills set at the employee model)
(注意:这甚至不需要accept_nested_attributes_for:在员工模型中设置的技能)
I am in the process of migrating my code to Rails 4.2 and moving to strong parameters. 我正在将我的代码迁移到Rails 4.2并转向强参数。
I've white-listed the skill_ids in the employees controller and invoking that on the update action, like so : 我在employees控制器中列出了skill_ids并在更新操作上调用了这个,如下所示:
def update
@employee = Employee.find(params[:id])
@employee.update_attributes(employee_params)
end
private
def employee_params
params.require(:employee).permit(:skill_ids, .....)
end
But it just wouldn't update the skill ids for the employees. 但它不会更新员工的技能ID。
Can someone please point me what has changed in Rails 4.2 for saving association values like these? 有人可以指出我在Rails 4.2中发生了哪些变化,以保存这些关联值?
thanks. 谢谢。
The issue was how I whitelisted the param. 问题是我如何将该参数列入白名单。 It should be whitelisted as an array param like so:
它应该被列入白名单,如下所示:
params.require(:employee).permit({:skill_ids => []}, .....)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.