Rails 3 嵌套模型形式:无法批量分配受保护的属性
我目前有一个使用嵌套模型设置的表单 - 到目前为止一切都按计划进行。该表单允许我创建销售,然后我可以创建客户和车辆(单独的模型)。
当我尝试创建一个注册号时,问题就出现了,这是一个与车辆嵌套的单独模型;本质上,我可以让一个文本框出现在表单上,但尝试创建注册号会导致控制台中出现 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您错误嵌套了资源,请参阅下面的箭头。
You mis-nested your resources, see arrow below.