Multiple Models In One Form

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs