2011-09-12 26 views
0

工作時,我認爲我嘗試建立一個嵌套形式嵌套形式不是的has_many

<%= semantic_form_for @order do |f| %> 
    <%= f.inputs :name => "Detail", :for => :order_detail do |od| %> 
    <%= od.input :shoe_id, :collection => Shoe.all.map{|s|[s.article_number,s.id]} %> 
    <%= od.input :size_id, :collection => Size.all.map{|s|[s.number,s.id]} %> 
    <%= od.input :color_id, :collection => Color.all.map{|c|[c.name,c.id]} %> 
    <%= od.input :quantity %> 
    <%- end -%> 
<%- end -%> 

它的工作原理我已經有了一個模型訂單

class Order < ActiveRecord::Base 
    has_many :order_details, :class_name => "OrderDetail" 
    accepts_nested_attributes_for :order_details 
end 

。但使用

:for => :order_details 

沒有。它什麼都不渲染。

==求助!

我找到了解決方案。

@ order.order_details是emtpy,因此不會呈現nested_form。

控制器書面方式:

@order.order_details.build 
+0

我不明白。當你這樣做的時候,它的工作方式是正確的,當你這樣做的時候是不正確的呢?對不起,我很困惑。 –

回答

0

我找到了解決辦法。

@ order.order_details是emtpy,因此不會呈現nested_form。

書面方式控制器:

@order.order_details.build 
0

fields_for用來渲染嵌套屬性,我認爲以下應工作

<%= semantic_form_for @order do |f| %> 
    <%= f.fields_for :order_details do |od| %> 
    <%= od.input :shoe_id, :collection => Shoe.all.map{|s|[s.article_number,s.id]} %> 
    <%= od.input :size_id, :collection => Size.all.map{|s|[s.number,s.id]} %> 
    <%= od.input :color_id, :collection => Color.all.map{|c|[c.name,c.id]} %> 
    <%= od.input :quantity %> 
<%- end -%> 

<% - 完 - %>

,我們可以用fields_for helper創建多個嵌套記錄 參見rails cast

http://railscasts.com/episodes/196-nested-model-form-part-1

http://railscasts.com/episodes/197-nested-model-form-part-2

0

我找到了解決辦法。

@order.order_details is emtpy so no nested_form is rendered. 

書面方式控制器:

@order.order_details.build 

會讓formtastic正確渲染