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.
Ryan W. (Guest)
on 2006-03-31 03: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.
David T. (Guest)
on 2006-03-31 03:24
(Received via mailing list)
On 31/03/06, Ryan W. <removed_email_address@domain.invalid> 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.