# Multiple Models In One Form

**URL:** <https://www.ruby-forum.com/t/multiple-models-in-one-form/134193>\
**Category:** Rails\
**Created:** [April 8, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/134193 "2008-04-08T19:37:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [April 8, 2008, 7:37pm UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/134193/1 "2008-04-08T19:37:25Z")

</div>

I’ve browsed several articles and tutorials for days and cannot figure  
out how to make this thing work the way I want it to. Basically I’m  
building a listings manager.

I want users to be able to create available details. For instance, if  
they’re doing listings for vehicles, they would create details for  
price, make, model, year, etc, and that is handled by my Details  
model. No problem.

Then, when a user creates a listing, I want the view to show the usual  
stuff for the listing (name, description, etc), I have a category  
wired up and working fine, but then I want those Details to show up  
with text boxes next to them so that they can fill out the details for  
a particular item. I’ve created a ListingDetail model which  
has :listing\_id, :detail\_id, and :value in it.

I cannot, after browsing all the tutorials out there, figure out how  
to do this. I’ll post the code I have below. It doesn’t work right,  
but perhaps someone can help?

# \_form.html.erb: and again this probably is laid out right…

**Name**  
 \<%= f.text\_field :name %\>

**Description**  
 \<%= f.text\_area :description, :rows =\> 4 %\>

**Category**  
 \<%= f.collection\_select(:category\_id, @categories, :id, :name) %\>

**Details**  
 \<% fields\_for :listing\_details do |d| %\> \<% @details.each do |detail| %\>

\<%= detail.name %\>: \<%= d.text\_field :value, :detail\_id =\> detail.id %\>

 \<% end %\> \<% end %\> 
# listings\_controller.rb which I have no idea what I’m supposed to

have here in order to save them all…

def create  
@listing = Listing.new(params[:listing])  
@listingdetail =  
@listing.listing\_details.build(params[:listing\_details])

```
respond_to do |format|
  if @listing.save
    flash[:notice] = 'Listing was successfully created.'
    format.html { redirect_to(admin_listings_url) }
    format.xml { render :xml => @listing, :status

```

=\> :created, :location =\> @listing }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @listing.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

# listing\_detail.rb

class ListingDetail \< ActiveRecord::Base  
belongs\_to :listing  
end

# listing.rb

class Listing \< ActiveRecord::Base  
belongs\_to :category  
has\_many :listing\_details  
end

# detail.rb

class Detail \< ActiveRecord::Base  
end

---

<div class="post-metadata">

**Author:** ![jeremy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/82dd89/32.png) [@jeremy](https://www.ruby-forum.com/u/jeremy)\
**Post date:** [April 9, 2008, 3:40am UTC](https://www.ruby-forum.com/t/multiple-models-in-one-form/134193/2 "2008-04-09T03:40:12Z")

</div>

I’m assuming you’ve read these.  
[http://www.railsforum.com/viewtopic.php?id=1063](http://www.railsforum.com/viewtopic.php?id=1063)  
[http://www.railsforum.com/viewtopic.php?id=1065](http://www.railsforum.com/viewtopic.php?id=1065)
