Reputation: 6466
I'm trying to make a site that has a form with one model ("Requests") nested in another ("Orders"). Specifically, I'd like the orders/new page to have a form composed of requests that a user can fill and then submit, so they're all associated with the order that is created.
On someone here's suggestion, I looked into the Railscast on the topic (#196), and I've run into a problem I can't figure out, so I figured I'd ask. The problem is, I'm following his directions, but the request forms just aren't showing up. The form fields associated with the Order model ARE showing up, however, and I'm really confused, cause I basically copied the Railscast verbatim. Thoughts?
Here's my form code. It's specifically the part in the fields_for tag that isn't showing up on my site:
<%= form_for(@order) do |f| %>
<% if @order.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@order.errors.count, "error") %> prohibited this order from being saved:</h2>
<ul>
<% @order.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= f.label :user_name %><br />
<%= f.text_field :user_name %>
</p>
<p>
<%= f.label :user_email %><br />
<%= f.text_field :user_email %>
</p>
<% f.fields_for :requests do |builder| %>
<p>
<%= builder.label :item, "Item" %><br />
<%= builder.text_field :item %>
</p>
<p>
<%= builder.label :lbs, "LBs" %> <br />
<%= builder.text_field :lbs %>
</p>
<% end %>
<p><%= f.submit "Submit Order" %></p>
<% end %>
And here's my code for the two models, Order first:
class Order < ActiveRecord::Base
attr_accessible :order_status, :price_estimate, :price_final, :user_email, :user_name, :user_phone
has_many :requests, :dependent => :destroy
accepts_nested_attributes_for :requests
end
class Request < ActiveRecord::Base
attr_accessible :item, :lbs, :notes, :order_id, :status
belongs_to :order
end
Finally, in case it's relevant, the bit I added, per the cast's suggestions, to my Order controller create action:
def new
@order = Order.new
3.times { @order.requests.build }
respond_to do |format| # this part I copied from a tutorial. But when I commented it out, it didn't change anything, so I don't think this is the problem
format.html # new.html.erb
format.json { render json: @order }
end
end
Help? I'm super confused by this. I'm guessing the problem might have something to do with the "builder," but I'm new to this, and an hour or so of fiddling around hasn't yielded much.
Really appreciate any help. (Also, as a post-script. I'm giving up and going to bed, to look at this tomorrow. Sorry if I don't follow up on any questions til then).
Thanks!
Upvotes: 0
Views: 47
Reputation: 115531
I see two mistakes here.
In the view, replace:
<% f.fields_for :requests do |builder| %>
With:
<%= f.fields_for :requests do |builder| %>
In your order modeil, replace:
attr_accessible :order_status, :price_estimate, :price_final, :user_email, :user_name, :user_phone
with:
attr_accessible :order_status, :price_estimate, :price_final, :user_email, :user_name, :user_phone, :requests_attributes
Upvotes: 1