Forum: Ruby on Rails DRY Rails question

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.
2addfb9463ae86d58614733ea16b0682?d=identicon&s=25 Ryan Williams (jimmyether)
on 2006-03-31 01:18
I need to pull user preferences on every page load of every controller
and action in my app. I do this like so:

@user = User.find(@session['user'].id)
@prefs = @user.prefs

And it works fine in the action of one of my controllers, but I
certainly don't want to plunk it in every instance. Where do you
normally put something like this? I tried putting it in the application
controller (application.rb), but it gives me the nil object error.
B26c571d17b3022ab1a98c1a223c32e6?d=identicon&s=25 David Thomson (Guest)
on 2006-03-31 01:24
(Received via mailing list)
On 31/03/06, Ryan Williams <ryan@jimmyether.com> wrote:
> I need to pull user preferences on every page load of every controller
> and action in my app. I do this like so:
>
> @user = User.find(@session['user'].id)
> @prefs = @user.prefs
>
> And it works fine in the action of one of my controllers, but I
> certainly don't want to plunk it in every instance. Where do you
> normally put something like this? I tried putting it in the application
> controller (application.rb), but it gives me the nil object error.

I'd suggest using it as a :before_filter in the application
controller, but you'll want to check that the @user object exists
before using it, of course.

David
This topic is locked and can not be replied to.