簡體   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