Rails 3 嵌套模型形式:无法批量分配受保护的属性

发布于 2024-11-25 01:30:30 字数 2186 浏览 1 评论 0原文

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

当我尝试创建一个注册号时,问题就出现了,这是一个与车辆嵌套的单独模型;本质上,我可以让一个文本框出现在表单上,​​但尝试创建注册号会导致控制台中出现 can not Mass allocate 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

and 和

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

and

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 %>

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

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).

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 can not mass assign protected attribute :registration_number error in the console, and when editing a sale that includes a vehicle with a registration number, the text box is empty.

The models involved are:

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

and

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

and

class RegistrationNumber < ActiveRecord::Base

  attr_accessible :number

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

end

and

class VehicleRegistrationNumber < ActiveRecord::Base

  belongs_to :vehicle
  belongs_to :registration_number

end

The form in question is:

<%= 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 %>

Any assistance would be greatly appreciated - thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

俯瞰星空 2024-12-02 01:30:30

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

<%= 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 %>

You mis-nested your resources, see arrow below.

<%= 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 %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文