1

我目前有一个使用嵌套模型设置的表单 - 到目前为止一切都按计划进行。该表单允许我创建销售,然后我可以创建客户和车辆(单独的模型)。

当我尝试创建一个注册号时出现问题,这是一个与车辆嵌套的单独模型;本质上,我可以在表单上显示一个文本框,但是尝试创建注册号会导致can not mass assign protected attribute :registration_number控制台出现错误,并且在编辑包含带有注册号的车辆的销售时,文本框为空。

涉及的模型有:

class Sale < 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

class Vehicle < ActiveRecord::Base
  attr_accessible :first_registration_date, :hidden, :registration_numbers_attributes

  has_many :sales
  has_many :customers, :through => :sales

  has_many :vehicle_registration_numbers, :dependent => :delete_all
  has_many :registration_numbers, :through => :vehicle_registration_numbers
  accepts_nested_attributes_for :registration_numbers, :allow_destroy => true

end

class RegistrationNumber < ActiveRecord::Base

  attr_accessible :number

  has_many :vehicle_registration_numbers, :dependent => :delete_all
  has_many :vehicles, :through => :vehicle_registration_numbers

end

class VehicleRegistrationNumber < ActiveRecord::Base

  belongs_to :vehicle
  belongs_to :registration_number

end

有问题的表格是:

<%= form_for @sale, :html => {:class => 'fullform'} do |f| %>

<%= field_set_tag 'Customer Details' do %>
    <%= f.fields_for :customer do |builder| %>
        <snip>
    <% end %>
<% end %>

<%= field_set_tag 'Vehicle Details' do %>
    <%= f.fields_for :vehicle do |vehicle_builder| %>
        <snip>
            <%= f.fields_for :registration_numbers do |registration_number_builder| %>
                <%= registration_number_builder.text_field :number, :class => 'formtxtbox-short' %>
            <% end %>
    <% end %>
<% end %>

<% end %>

任何帮助将不胜感激 - 谢谢!

4

1 回答 1

3

您错误地嵌套了资源,请参见下面的箭头。

<%= field_set_tag 'Vehicle Details' do %>
  <%= f.fields_for :vehicle do |vehicle_builder| %>
    <snip>
 ===>> <%= vehicle_builder.fields_for :registration_numbers do |registration_number_builder| %>
            <%= registration_number_builder.text_field :number, :class => 'formtxtbox-short' %>
        <% end %>
  <% end %>
<% end %>
于 2011-07-16T22:59:42.603 回答