How to display user info on each view?

Hi !

Hi have the following problem, and not sure about the best way to solve
this in rails. All my views are wrapped in two different layouts. These
layouts should display something like 'your are logged as ', followed by
some user info.

As this info should appear on all actions of my app (except login /
logout), what’s the good way to pass the info from the controller to all
views :

  • When the User log in, store the User object in the session instead of
    just the username ? Thus I could ask all info about the user from the
    data in session.

  • Define an action to be called before any action (in application.rb
    just to put @current_user)

  • Another way ?

Thanks in advance,


i would suggest to add a before_filter to the application controller,
and get the user object there, from the user id stored in the session.

then in your layout you can always access the user object, and e.g.
render a partial that is showing the user info stuff in all layouts.

Storing the whole user object is not a good idea if your app is going
to have many users, and your users will have a bigger number of columns
than just the basic name-pass-id-created_at etc … that will just make
the session data too big…