我有以下一段代码,它在Rails 4.1中使用protected_attributes gem工作得很好(我还没有将我的代码移动到strong_parameters)
models/employee.rb
class Employee
has_and_belongs_to_many :skills
attr_accessible :skill_ids, ...
end
models/skill.rb
class Skill
has_and_belongs_to_many :employees
end
当我更新员工时,我将技能与员工绑定,因此我的视图如下所示:
视图/员工/_form.html.erb
<%= form_for @employee, do |f| %>
.....
<%= f.collection_select :skill_ids, Skill.all, :id, :name, {},
{:multiple => true, class: 'select2 '} %>
......
<% end %>
技能编号 (skill_ids)是attr_accessible参数的一部分,因此在保存员工表单时可以完美地使用。
(注:这甚至不需要在员工模型上设置accepts_nested_attributes_for :skills)Rails 4.2
我正在将代码迁移到Rails 4.2并切换到strong parameters。
我在员工控制器中列出了skill_ids,并在更新操作中调用它,如下:
controllers/employee_controller.rb
def update
@employee = Employee.find(params[:id])
@employee.update_attributes(employee_params)
end
private
def employee_params
params.require(:employee).permit(:skill_ids, .....)
end
但它无法更新员工的技能ID。
有人能告诉我在Rails 4.2中保存类似这样的关联值有哪些变化吗?
谢谢。