Rails 4.2 storing association IDs has_and_belongs_to_many

I have the following bit of code that works fine with Rails 4.1 with the protected_attributes gem (I haven't had any code ported to strong_parameters yet)

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

      

I bind skills to employee when updating employee, so my view looks like below

view / employees / _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_accessable params, so it worked great when saving employee form. (Note: this doesn't even require accepts_nested_attributes_for: skills set on employee model)

Rails 4.2

I am porting my code to Rails 4.2 and moving on to strong options.

I have a salesperson whitelist in my employee controller and gets called about this on update:

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

      

But it just won't update the skill IDs for employees.

Can anyone point me to what has changed in Rails 4.2 to preserve association values ​​like this?

thank.

+3


source to share


1 answer


The problem was how I included the param parameter. It should be whitelisted as an array, for example:



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

      

+5


source







All Articles