HighDots Forums  

Create multiple objects in 1 form

Ruby On Rails Talk Ruby On Rails programming language mailing list


Discuss Create multiple objects in 1 form in the Ruby On Rails Talk forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
eggie5
 
Posts: n/a

Default Create multiple objects in 1 form - 11-06-2009 , 11:33 PM






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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
  #2  
Old   
Todd A. Jacobs
 
Posts: n/a

Default Re: Create multiple objects in 1 form - 11-07-2009 , 02:41 AM






On Fri, Nov 06, 2009 at 08:33:02PM -0800, eggie5 wrote:


Quote:
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"


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk (AT) googlegroups (DOT) com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe (AT) googlegroups (DOT) com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.