Forum: Ruby on Rails Best practice for passing vars etc...

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Elliott G. (Guest)
on 2009-04-07 23:26
(Received via mailing list)
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.
Frederick C. (Guest)
on 2009-04-08 00:33
(Received via mailing list)
On Apr 7, 8:24 pm, elliottg <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
Elliott G. (Guest)
on 2009-04-08 21:18
(Received via mailing list)
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>
This topic is locked and can not be replied to.