Search code examples
ruby-on-railsrubyruby-on-rails-3erbformtastic

Nested form not working with has_many


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.name,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

Solution

  • I found the solution.

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

    writting in the controller:

    @order.order_details.build