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
ビューページには、次の複数のエントリ用に入れ子になったレールフォームがあります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
まだエラーがあります。また、オンラインでチケットに複数を追加する<%= f.fields_for :ticket do |t| %>
と、quantity
フィールドが表示されません。