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.... <p> <b>Name</b><br /> <%= f.text_field :name %> </p>
<p> <b>Description</b><br /> <%= f.text_area :description, :rows => 4 %> </p>
<p> <b>Category</b><br /> <%= f.collection_select(:category_id, @categories, :id, :name) %> </p>
<p> <b>Details</b><br /> <% fields_for :listing_details do |d| %> <% @details.each do |detail| %> <p> <%= detail.name %>: <%= d.text_field :value, :detail_id => detail.id %> </p> <% end %> <% end %> </p>
# 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