简体   繁体   English

Rails 4.2保存has_and_belongs_to_many关联ID

[英]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)

models/employee.rb 车型/ employee.rb

class Employee

   has_and_belongs_to_many :skills

   attr_accessible :skill_ids, ...

end

models/skill.rb 车型/ skill.rb

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 我在更新员工时将技能绑定到员工,因此我的视图如下所示

views/employees/_form.html.erb 意见/员工/ _form.html.erb

 <%= 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:在员工模型中设置的技能)

Rails 4.2 Rails 4.2

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并在更新操作上调用了这个,如下所示:

controllers/employee_controller.rb 控制器/ 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

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.

相关问题 rails 4-使用has_and_belongs_to_many关联 - rails 4 - using has_and_belongs_to_many association rails 中 has_and_belongs_to_many 关联的问题 - issue in has_and_belongs_to_many association in rails Rails 4 - has_and_belongs_to_many关联的复选框 - Rails 4 - checkboxes for has_and_belongs_to_many association Rails has_and_belongs_to_many在2级关联 - Rails has_and_belongs_to_many on 2 level association Rails has_and_belongs_to_many关联 - Rails has_and_belongs_to_many association Rails:has_and_belongs_to_many关联中collection_singular_ids =方法的返回类型和值是什么? - Rails: What is the return type and value of the collection_singular_ids= method in a has_and_belongs_to_many association? 找到包含多个 ID 的 has_and_belongs_to_many 关联 - find has_and_belongs_to_many association with multiple ids inclusive rails association - has_many vs has_and_belongs_to_many - rails association - has_many vs has_and_belongs_to_many Rails 3 has_and_belongs_to_many关联:如何分配相关对象而不将其保存到数据库 - Rails 3 has_and_belongs_to_many association: how to assign related objects without saving them to the database Ruby on Rails-has_and_belongs_to_many协会的委托devise成员 - Ruby on Rails - delegate devise member on has_and_belongs_to_many association
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM