# Newbie struggling with associations and url passing

**URL:** <https://www.ruby-forum.com/t/newbie-struggling-with-associations-and-url-passing/133803>\
**Category:** Rails\
**Created:** [April 3, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/newbie-struggling-with-associations-and-url-passing/133803 "2008-04-03T20:09:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bskaggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bskaggs](https://www.ruby-forum.com/u/bskaggs)\
**Post date:** [April 3, 2008, 8:09pm UTC](https://www.ruby-forum.com/t/newbie-struggling-with-associations-and-url-passing/133803/1 "2008-04-03T20:09:08Z")

</div>

hi

i am stuck!

i have a products controller, model and list view.  
model states: has\_many :versions  
db table: id, title

i have a versions controller, model and list view.  
model states: belongs\_to :products  
db table: id, num, product\_id (hopefully the foreign key!)

i want to know how, in this scenario…

a user selects a product, say microsoft excel → versions list for excel  
are displayed. IVE GOT THIS WORKING!

the passed url is: [http://localhost:3000/product/show/1](http://localhost:3000/product/show/1)

but,

supposing i want to add a new version for excel, say program id =\> ‘1’,  
how do i do this?

on the view, i have a button “add new version”

this in turn calls a create method in the controller, but i am unsure  
how i can pass the product\_id across to populate the foreign key in the  
versions table. how can i get the product number, when it is only know  
from the URL, when passed to enter the associated versions page?

from the view:

# Add new version
\<%= start\_form\_tag :action =\> 'create' %\> 

Number: \<%= text\_field 'version', 'number' %\>

 \<%= submit\_tag "Create" %\> \<%= end\_form\_tag %\>

from the version controller:

# create a new version

def create  
@version = Version.new(params[:version])  
if @version.save  
redirect\_to :controller =\> ‘product’, :action =\> ‘show’, :id =\>  
@version.product.id  
end  
end

can you help? do u need more info?

HELP!!!

i cant find any similar tutorials on this kind of association! i bought  
the agile dev book, no real use!

can you recommened any good sites i could try? or books?

---

<div class="post-metadata">

**Author:** ![bskaggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bskaggs](https://www.ruby-forum.com/u/bskaggs)\
**Post date:** [April 3, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/newbie-struggling-with-associations-and-url-passing/133803/2 "2008-04-03T20:28:14Z")

</div>

In your product ‘show’ view, for that link to add a new version, it can  
look something like this:

\<%= link\_to new\_version\_path(:product =\> @product.id) %\>

Clicking that link routes the flow to the versions controller new  
method. In that method, you’d peek at the params to see if the product  
was specified, and set the version field if it was:

def new  
@version = Version.new  
@version.product\_id = params[:product] if params[:product]  
respond\_to do |format|  
format.html # new.html.erb  
format.xml { render :xml =\> @scenario }  
end  
end

which then routes you the the ‘new’ view for version. product\_id is  
already set.

---

<div class="post-metadata">

**Author:** ![bskaggs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/3ab097/32.png) [@bskaggs](https://www.ruby-forum.com/u/bskaggs)\
**Post date:** [April 4, 2008, 1:02am UTC](https://www.ruby-forum.com/t/newbie-struggling-with-associations-and-url-passing/133803/3 "2008-04-04T01:02:55Z")

</div>

Hi!  
This is assuming you’ve got @product set to the product in question in  
your CALLING view, and that your version controller is called  
VersionController (version\_controller.rb):

# Add new version
\<%= start\_form\_tag :controller =\> 'version', :action =\> 'create', :product\_id =\> @product.id %\> 

Number: \<%= text\_field 'version', 'number' %\>

 \<%= submit\_tag "Create" %\> \<%= end\_form\_tag %\>
# create a new version

def create  
@version = Version.new(params[:version])  
@version.product\_id = params[:product\_id]  
if @version.save  
redirect\_to :controller =\> ‘product’, :action =\> ‘show’, :id =\>  
@version.product.id  
end  
end

The lines to look out for (ie the different ones) are:  
\<%= start\_form\_tag :controller =\> ‘version’, :action =\>  
‘create’, :product\_id =\> @product.id %\>  
and  
@version.product\_id = params[:product\_id]

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3  
OUT APRIL 6  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
