transaction.rb
模型:
class Transaction < ActiveRecord::Base
attr_accessible :customer, :tickets_attributes
has_many :tickets
accepts_nested_attributes_for :tickets
end
ticket.rb
模型:
class Ticket < ActiveRecord::Base
attr_accessible :booking_id, :quantity, :transaction_id
belongs_to :transaction
belongs_to :booking
end
在视图页面中,我有一个用于多个条目的嵌套 rails 表单ticket
:
<%= form_for(@transaction) do |f| %>
<%= f.text_field :customer %>
<% @sezzion.bookings.each do |booking| %>
<%= booking.bookingdate %>:
<%= f.fields_for :ticket do |t| %>
<%= t.text_field :quantity, :value => 0, :class => "quantity" %>
<%= t.hidden_field :booking_id, :value => booking.id %>
<% end %>
<% end %>
<%= f.submit "create transaction" %>
<% end %>
当我提交表单时,出现以下错误:
ActiveModel::MassAssignmentSecurity::Error in TransactionsController#create
Can't mass-assign protected attributes: ticket
我有attr_accessible :tickets_attributes
和accepts_nested_attributes_for :tickets
在transaction
模型中,但仍然有错误。此外,当我在 line 上添加复数时<%= f.fields_for :ticket do |t| %>
,该quantity
字段不显示。