繁体   English   中英

Rails 4.2保存has_and_belongs_to_many关联ID

[英]Rails 4.2 saving has_and_belongs_to_many association Ids

我有以下一些代码,使用带有protected_attributes gem的Rails 4.1正常工作(我没有将我的代码移到strong_parameters)

车型/ employee.rb

class Employee

   has_and_belongs_to_many :skills

   attr_accessible :skill_ids, ...

end

车型/ 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 params的一部分,因此它在保存员工表单时工作得很好。 (注意:这甚至不需要accept_nested_attributes_for:在员工模型中设置的技能)

Rails 4.2

我正在将我的代码迁移到Rails 4.2并转向强参数。

我在employees控制器中列出了skill_ids并在更新操作上调用了这个,如下所示:

控制器/ 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中发生了哪些变化,以保存这些关联值?

谢谢。

问题是我如何将该参数列入白名单。 它应该被列入白名单,如下所示:

 params.require(:employee).permit({:skill_ids => []}, .....)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM