我有一個嵌套的表單,它拒絕向數據庫中插入新記錄,儘管父插入正常。無疑是一個明顯的,但有人可以建議爲什麼?嵌套表單沒有插入到數據庫中?
父模型:
class Delivery < ActiveRecord::Base
attr_accessible :orders_attributes
has_many :orders, as: :orderable
accepts_nested_attributes_for :orders
嵌套模式
class Order < ActiveRecord::Base
attr_accessible :info
belongs_to :orderable, polymorphic: true
belongs_to :delivery
父控制器
def new
@delivery = Delivery.new
order = @delivery.orders.build
respond_to do |format|
format.html # new.html.erb
format.json { render json: @delivery }
end
end
形式
= form_for @delivery do |f|
= fields_for :orders do |builder|
= builder.label :info
= builder.text_area :info
.actions
= f.submit
輸出:
在2013年2月13日16時06分53秒0100 處理由DeliveriesController#發起者POST 「/交付」 爲127.0.0.1創建以HTML參數: { 「UTF8」=> 「✓」, 「authenticity_token」=>「MdNjphnBQaaHdxelT7RnWDNG2XPpDTQipDKAOkT57h0 =」, 「orders」=> {「info」=>「1222」},「commit」=>「Create Delivery」}(0.1ms) begin transaction SQL (2.7ms)INSERT INTO「deliveries」 (「created_at」,「updated_at」)VALUES(?,?)[[「created_at」,Wed,13 Feb 2013 15:06:53 UTC +00:00],[ 「updated_at」,2013年2月13日,星期三 15:06:53 UTC +00:00]](1.3ms)commit transaction Redir反映到
您可以看到它發佈了訂單數據,但它沒有正確地將其放入數據庫中......我錯過了什麼?
這個工作,非常感謝! – Nick 2013-02-13 15:28:22
歡迎您 - 我看到您在這裏相當新,所以可能不知道您應該接受任何您認爲是正確的答案。這有助於其他使用Google的人,並增強了答案的聲譽。 – chrispanda 2013-02-13 15:36:43
完成 - 由於某種原因,它不會讓我做10分鐘,但它現在已經奏效! – Nick 2013-02-13 15:41:03