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?