0
我正在嘗試爲測試目的手動構建表單域。我有以下型號:如何使用accept_nested_attributes_for手動構建正確的嵌套窗體?
class Bedroom < ActiveRecord::Base
has_many :booked_bedrooms
has_many :bookings, :through => :booked_bedrooms
end
class Booking < ActiveRecord::Base
has_many :booked_bedrooms
has_many :bedrooms, :through => :booked_bedrooms
accepts_nested_attributes_for :booked_bedrooms
end
class BookedBedroom < ActiveRecord::Base
belongs_to :booking
belongs_to :bedroom
# fields: bedroom_id, :booking_id
end
當我嘗試在控制檯下面,登記,相關booked_bedroom獲取保存:
>> b = Booking.new({ :booked_bedrooms_attributes => { 0 => { :bedroom_id => 1 } } })
=> #<Booking id: nil, start_date: nil, end_date: nil, created_at: nil, updated_at: nil>
>> b.save
=> true
>> b.booked_bedrooms
=> [#<BookedBedroom id: 1, booking_id: 1, bedroom_id: 1, created_at: "2010-06-22 18:55:57", updated_at: "2010-06-22 18:55:57">]
所以我建立這樣一種形式:
<% form_for @booking do |form| %>
<% for bedroom in @available_bedrooms %>
<%= check_box_tag "booked_bedrooms_attributes[#{bedroom.id}][bedroom_id]", bedroom.id %> <%= bedroom.name %>
as
<%= select_tag "booked_bedrooms_attributes[#{bedroom.id}][booking_type_id]", options_for_select(bedroom.booking_types.map {|p| [p.name, p.id]}) %>
<% end %>
<% end %>
但這不行。預訂得到保存,但沒有新的預訂臥室記錄。有人能告訴我什麼是錯的嗎?
我知道我可以使用formtastic或一些fields_for,但我想解決硬編碼形式的問題,用於演示目的。
咄,初學者的錯誤..謝謝;) – pduersteler 2010-06-23 10:06:53