# Best practice for passing vars etc

**URL:** <https://www.ruby-forum.com/t/best-practice-for-passing-vars-etc/164963>\
**Category:** Rails\
**Created:** [April 7, 2009, 9:26pm UTC](https://www.ruby-forum.com/t/best-practice-for-passing-vars-etc/164963 "2009-04-07T21:26:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [April 7, 2009, 9:26pm UTC](https://www.ruby-forum.com/t/best-practice-for-passing-vars-etc/164963/1 "2009-04-07T21:26:04Z")

</div>

I am new to rails and I am having a hard time coming up with a clean  
solution to handle passing vars to controllers and views so that they  
will have the data they need to function correctly.

For instance. I have products\_controller. This controller manages a  
Product model, and Product belongs\_to Category.

Within products\_controller all CRUD actions are scoped thru the  
Categories has\_many association IE:

category = Category.find\_by\_name params[:category]  
@product = category.products.find(params[:id])

In order for the products\_controller actions to operate they all need  
access to params[:category]. Currently I am passing that attribute  
around thru a combination of hidden for\_for fields and stuff like  
this:  
link\_to ‘Edit’, edit\_product\_path(product, :category =\>params  
[:category])

I am running into a lot of issues keeping track of passing this info  
around through the various views and actions that are in play. It  
feels sloppy and prone to problems. It’s like I’m working in PHP  
again.

Should I use global varibles or sessions to solve these kinds of  
problems?  
Whats the best thing to do here?

Thanks a lot.

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [April 7, 2009, 10:33pm UTC](https://www.ruby-forum.com/t/best-practice-for-passing-vars-etc/164963/2 "2009-04-07T22:33:39Z")

</div>

On Apr 7, 8:24 pm, elliottg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> again.

You might want to read up on nested resources

> Should I use global varibles or sessions to solve these kinds of  
> problems?

I really wouldn’t - that’s definitely grungy (both server side and  
from the point of view of the user)

Fred

---

<div class="post-metadata">

**Author:** ![elliottg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b77776/32.png) [@elliottg](https://www.ruby-forum.com/u/elliottg)\
**Post date:** [April 8, 2009, 7:18pm UTC](https://www.ruby-forum.com/t/best-practice-for-passing-vars-etc/164963/3 "2009-04-08T19:18:44Z")

</div>

Thanks Fredrick.

The tip on nested routes was just what I needed.

On Apr 7, 4:32 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
