I've got a model Order
class Order < ActiveRecord::Base
has_many :order_details, :class_name => "OrderDetail"
accepts_nested_attributes_for :order_details
end
When in my view I try to build a nested form
<%= 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.na开发者_开发技巧me,c.id]} %>
<%= od.input :quantity %>
<%- end -%>
<%- end -%>
It works. But using
:for => :order_details
does not. It renders nothing.
== SOLVED!!
I found the solution.
@order.order_details is emtpy so no nested_form is rendered.
writting in the controller:
@order.order_details.build
fields_for is used to rendering nested attributes,i think following should work
<%= 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 -%>
<%- end -%>
and we can create multiple nested record with fields_for helper see rails cast
http://railscasts.com/episodes/196-nested-model-form-part-1
http://railscasts.com/episodes/197-nested-model-form-part-2
I found the solution.
@order.order_details is emtpy so no nested_form is rendered.
writting in the controller:
@order.order_details.build
will make formtastic render correctly
I found the solution.
@order.order_details is emtpy so no nested_form is rendered.
writting in the controller:
@order.order_details.build
精彩评论