Rails - Accepts_nested_attributes_for bulk assignment error

I'm currently trying to customize a form with nested fields in relation to belongs_to, but I encountered a bulk assignment error. My code so far looks like this (some html removed):

Selling Model:

class Sale < ActiveRecord::Base attr_accessible :customer_attributes belongs_to :customer accepts_nested_attributes_for :customer end 

new.html.erb:

 <div class="container"> <%= form_for :sale, :url => sales_path do |sale| -%> <%= sale.fields_for :customer do |customer_builder| %> <%= render :partial => "customers/form", :locals => {:customer => customer_builder, :form_actions_visible => false} %> <% end -%> <% end -%> 

clients / _form.html.erb

 <fieldset> <label class="control-label">Customer Type</label> <%= collection_select(:customer, :customer_type_id, CustomerType.all, :id, :value, {}, {:class => "chzn-select"}) %> </fieldset> 

I believe this should allow me to create a Sale object and a nested Customer object. Sent parameters (note that some unrelated parameters are included):

 {"utf8"=>"✓", "authenticity_token"=>"qCjHoU9lO8VS060dXFHak+OMoE/GkTMZckO0c5SZLUU=", "customer"=>{"customer_type_id"=>"1"}, "sale"=>{"customer"=>{"features_attributes"=>{"feature_type_id"=>"1", "value"=>"jimmy"}}}, "vehicle"=>{"trim_id"=>"1", "model_year_id"=>"1"}} 

The error I am getting is:

 Can't mass-assign protected attributes: customer 

I can understand why this could be so, because: the client is not on the attr_accessible list for Sale - although should the form not send client attributes instead of the client?

Any help / advice appreciated.

EDIT 1: As far as I can tell, the attr_accessible in the Sale model should be covered: customer_attributes - if someone says differently, please let me know.

EDIT 2: I tried various permutations, but I cannot force the parameters to send client_attributes instead of a simple client - maybe I missed the tag or used the wrong tag somewhere in the forms above?

EDIT 3: I found another question about SO that indicated a problem with the part :url => on the form_for tag - the question was about formatting, but I wonder if this could be the reason the problem is here?

+3
source share
2 answers

In the end, I got an answer. The key was the following line:

 <%= collection_select(:customer, :customer_type_id, CustomerType.all, :id, :value, {}, {:class => "chzn-select"}) %> 

which must be changed to:

 <%= customer.collection_select(:customer_type_id, CustomerType.all, :id, :value, {}, {:class => "chzn-select"}) %> 

As soon as this was changed, everything fell into place.

+1
source

This could be a problem ... from the API docs:

Usage with attr_accessible

Using attr_accessible can interfere with nested attributes if you are not careful. For example, if you used the member model above attr_accessible:

attr_accessible :name

You will need to change it to look like this:

attr_accessible :name, :posts_attributes

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#label-Using+with+attr_accessible

+4
source

Source: https://habr.com/ru/post/1447354/


All Articles