I have an model in my rails app called Dog. I want to create a form where I can create an arbitrary amount of dogs with one request. Here''s the default for creating a dog form: <h1>New dog</h1> <% form_for(@dog) do |f| %> <%= f.error_messages %> <%= f.label :confirmations %><br /> <%= f.text_field :confirmations %> </p> <p> <%= f.label :name%><br /> <%= f.text_field :name%> </p> <p> <%= f.submit ''Create'' %> </p> <% end %> with this controller code: def new @dog= Dog.new respond_to do |format| format.html # new.html.erb format.xml { render :xml => @dog} end end def create @dog= Dog.new(params[:dog]) respond_to do |format| if @ dog.save flash[:notice] = ''dog was successfully created.'' format.html { redirect_to(@ dog) } format.xml { render :xml => @ dog, :status => :created, :location => @ dog} else format.html { render :action => "new" } format.xml { render :xml => @ dog.errors, :status => :unprocessable_entity } end end end So here I have a form where I can create a dog in the DB. How do I create a form (and the respective controller code) to create 2 or 3 dogs?
On Fri, Nov 06, 2009 at 08:33:02PM -0800, eggie5 wrote:> So here I have a form where I can create a dog in the DB. How do I > create a form (and the respective controller code) to create 2 or 3 > dogs?While I''m admittedly a rails newcomer--and probably destined to go back to PHP if I can''t get some traction on some of my own forms issues--I can think of two practical ways to go about this: 1. Have a successful create add some feedback to flash[:notice] and redirect to another dog entry form. As long as the session (or some other mechanism) is populating a foreign key into each dog object so you can find the records again, you could do this an arbitrary number of times. 2. Use AJAX to get the number of dogs you want to enter, and then create a nested form for each one. Maybe some of the more experienced rails folk will have better suggestions for you. -- "Oh, look: rocks!" -- Doctor Who, "Destiny of the Daleks"