Reputation: 2405
I am working on Rails 3.1.1.rc3 and I have 2 classes as shown below.
class Customer < ActiveRecord::Base
has_many :orders, :dependent => :destroy
accepts_nested_attributes_for :orders
end
class Order < ActiveRecord::Base
belongs_to :customer
end
In my form:
<%= form_for(@customer) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name,:class=>'textbox' %>
</div>
<%= f.fields_for :orders do |order_form| %>
<div class="field">
<%= order_form.label :number %><br />
<%= order_form.text_field :number, :class=>'textbox' %>
</div>
<%end%>
<%end%>
When it renders, the first part of the form (for customer) shows up, but the second part (for order) doesn't. Any pointers are appreciated. Thanks.
Upvotes: 0
Views: 553
Reputation: 8744
add
<%= f.fields_for :orders do |order_form| %>
instead of
<% f.fields_for :orders do |order_form| %>
Edit: have a look at how fields_for is defined (there are examples there)
Upvotes: 1
Reputation: 84114
Two things. First fields_for basically iterates over customer.orders so if there are no orders you'll get no output. If you just want some blank fields for users to input order details you'd typically stick
@customer.orders.build
In your controller. Secondly, fields_for
is very similar to form_for
, you need to use <%= for it too
Upvotes: 4