# New View Example With Child Object?

**URL:** <https://www.ruby-forum.com/t/new-view-example-with-child-object/61361>\
**Category:** Rails\
**Created:** [January 14, 2006, 12:43am UTC](https://www.ruby-forum.com/t/new-view-example-with-child-object/61361 "2006-01-14T00:43:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dstocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dstocks](https://www.ruby-forum.com/u/dstocks)\
**Post date:** [January 14, 2006, 12:43am UTC](https://www.ruby-forum.com/t/new-view-example-with-child-object/61361/1 "2006-01-14T00:43:45Z")

</div>

Could somebody point me to some example code? This must be simple, I  
just can’t find any examples. I need a new view (new.rhtml) to include  
the creation of a child object. I have a table for servers (Servers)  
and a table for IP Addresses (Ips).

Server  
has\_many :ips  
Ip  
belongs\_to :server

In my new.rhtml I want the IP entered to become a record in the Ips  
table. The Ips table is very simple. It has two rows, id and  
server\_id. Where would I look for example code for new.rhtml and  
server\_controller.rb?

Thanks for the help!

- Don

---

<div class="post-metadata">

**Author:** ![dstocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dstocks](https://www.ruby-forum.com/u/dstocks)\
**Post date:** [January 17, 2006, 1:05am UTC](https://www.ruby-forum.com/t/new-view-example-with-child-object/61361/2 "2006-01-17T01:05:12Z")

</div>

Any suggestions here? Thanks.

- Don

---

<div class="post-metadata">

**Author:** ![dstocks](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dstocks](https://www.ruby-forum.com/u/dstocks)\
**Post date:** [January 17, 2006, 3:07am UTC](https://www.ruby-forum.com/t/new-view-example-with-child-object/61361/3 "2006-01-17T03:07:34Z")

</div>

On 1/16/06, Don S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Any suggestions here? Thanks.
> 
> - Don

Here’s an example that handles a one-to-one relationship between  
servers.  
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\_controller.rb  
model :server  
model :ip

def new  
@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  
end  
end

#new.rhtml  
#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  
params[:server].

Saving the parent object (@server) also saves any unsaved children, so  
the new Server and Ip objects are both saved in one step.
