On 1/16/06, Don S. [email protected] wrote:
Any suggestions here? Thanks.
Here’s an example that handles a one-to-one relationship between
Since you need multiple IPs per server, you’ll want to work out some
way to address that in the view. One way would be with Ajax and/or
RJS templates; another would be to let the user enter a
comma-separated list of IPs… yet another would be to ask how many IPs
there will be up front, before showing the ‘new’ template, and build
that many form entries via a partial.
@server = Server.new(params[:server])
@ip = Ip.new(params[:ip])
@server.ips << @ip
if request.post? and @server.save
flash[:notice] = “All systems Go.”
redirect_to :action => ‘show’, :id => @server
#I’ve omitted various useful things like labels, help text, etc.
<%= start_form_tag :action => ‘new’ %>
<%= text_field ‘server’, ‘some_property’ %>
<%= select ‘server’, ‘something_else’, some_list_of_options %>
<%= text_field ‘ip’, ‘address’ %>
<%= submit_tag “Create Server” %>
<%= end_form_tag %>
Every input field value for ‘ip’ will be available in params[:ip] once
this form is posted, and every ‘server’ input field will be found in
Saving the parent object (@server) also saves any unsaved children, so
the new Server and Ip objects are both saved in one step.