# Best practice for passing vars etc...

**URL:** <https://discuss.rubyonrails.org/t/best-practice-for-passing-vars-etc/41318>\
**Category:** rubyonrails-talk\
**Created:** [April 7, 2009, 7:24pm UTC](https://discuss.rubyonrails.org/t/best-practice-for-passing-vars-etc/41318 "2009-04-07T19:24:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![elliottg](https://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@elliottg](https://discuss.rubyonrails.org/u/elliottg)\
**Post date:** [April 7, 2009, 7:24pm UTC](https://discuss.rubyonrails.org/t/best-practice-for-passing-vars-etc/41318/1 "2009-04-07T19:24:52Z")

</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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [April 7, 2009, 8:32pm UTC](https://discuss.rubyonrails.org/t/best-practice-for-passing-vars-etc/41318/2 "2009-04-07T20:32:50Z")

</div>

> 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.

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://avatars.discourse-cdn.com/v4/letter/e/b77776/32.png) [@elliottg](https://discuss.rubyonrails.org/u/elliottg)\
**Post date:** [April 8, 2009, 5:18pm UTC](https://discuss.rubyonrails.org/t/best-practice-for-passing-vars-etc/41318/3 "2009-04-08T17:18:03Z")

</div>

Thanks Fredrick.

The tip on nested routes was just what I needed.
