Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3 Nested Model Form: Can not mass assign protected attribute
    primarykey
    data
    text
    <p>I currently have a form set up with nested models - all going according to plan so far. The form allows me to create a sale, and from that I can create a customer and a vehicle (separate models). </p> <p>The problem comes when I try to create a registration number, which is a separate model nested from vehicle; essentially I can get a text box to appear on the form, but trying to create a registration number results in a <code>can not mass assign protected attribute :registration_number</code> error in the console, and when editing a sale that includes a vehicle with a registration number, the text box is empty. </p> <p>The models involved are:</p> <pre><code>class Sale &lt; ActiveRecord::Base attr_accessible :customer_id, :vehicle_id, :sale_date, :customer_attributes, :vehicle_attributes belongs_to :customer accepts_nested_attributes_for :customer belongs_to :vehicle accepts_nested_attributes_for :vehicle end </code></pre> <p>and</p> <pre><code>class Vehicle &lt; ActiveRecord::Base attr_accessible :first_registration_date, :hidden, :registration_numbers_attributes has_many :sales has_many :customers, :through =&gt; :sales has_many :vehicle_registration_numbers, :dependent =&gt; :delete_all has_many :registration_numbers, :through =&gt; :vehicle_registration_numbers accepts_nested_attributes_for :registration_numbers, :allow_destroy =&gt; true end </code></pre> <p>and</p> <pre><code>class RegistrationNumber &lt; ActiveRecord::Base attr_accessible :number has_many :vehicle_registration_numbers, :dependent =&gt; :delete_all has_many :vehicles, :through =&gt; :vehicle_registration_numbers end </code></pre> <p>and</p> <pre><code>class VehicleRegistrationNumber &lt; ActiveRecord::Base belongs_to :vehicle belongs_to :registration_number end </code></pre> <p>The form in question is:</p> <pre><code>&lt;%= form_for @sale, :html =&gt; {:class =&gt; 'fullform'} do |f| %&gt; &lt;%= field_set_tag 'Customer Details' do %&gt; &lt;%= f.fields_for :customer do |builder| %&gt; &lt;snip&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;%= field_set_tag 'Vehicle Details' do %&gt; &lt;%= f.fields_for :vehicle do |vehicle_builder| %&gt; &lt;snip&gt; &lt;%= f.fields_for :registration_numbers do |registration_number_builder| %&gt; &lt;%= registration_number_builder.text_field :number, :class =&gt; 'formtxtbox-short' %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Any assistance would be greatly appreciated - thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload