Question concerning controllers and a global layout

Hi Oliver,

In your controller, in a before_filter, assign an object that contains
all your user data to, say, @user. In your layout (or in a partial
which is rendered from your layout), simply refer to this variable.
e.g., to get the user name.


Yes, you should be able to render that with <%= render :partial =>
'shared/user_data' %>, for example.

Good luck!

George Ogata wrote:
>> - I can access the current_user from everywhere, cause I use
>> acts_as_authenticated.
>> - And I render a partial from app/views/shared for example, that uses
>> that information, right?
> Yes, you should be able to render that with <%= render :partial =>
> 'shared/user_data' %>, for example.

Is it even possible to call a controller method that way? I have to to
do a little bit more then expected in this thingy and I don'T want to
link my User.model each and everywhere. :slight_smile:

Hi Oliver,

I'm afraid I don't quite understand your problem. That call to
#render in the layout does not "call a controller method"; it simply
renders a partial at the point at which it appears in the layout. In
this case, it'd actually be almost equivalent to replacing the call to
#render with the contents of the partial.

I'm not sure I understand what you mean by "I don'T want to link my
User.model each and everywhere" either. If you have all your user
data in User.model, then I would do something like:

class ApplicationController
  def setup_user
    @user = User.model
  before_filter :setup_user

in your controller, and use @user.blah in your views/layouts/partials.
This should work fine with or without AJAX, as before_filters are run
for both XML and ordinary HTTP requests.

So, something like a controller, where I can encapsulate the logic and a
view (partial) to render the results would be better.

This sounds like the other approach that comes to mind: components.
Components are deprecated in the core, although I believe they're
available as a plugin somewhere. With a component, you basically say
in your view "insert the output of this controller action here".
Perhaps that's what you're seeking.

I ask, cause so far rails/my app resists to every "brilliant plan" I
have to get something like that. Or would you put the controller logic
in this case into the view? Or in the before_filter? The problem I have
with the before_filter, I like to periodically update this thingy using
AJAX, ...

I don't see the need to put controller logic into the view.
before_filters exist on controllers, so it would be perfectly
legitimate IMHO to put "[controller logic] in the before_filter".
AJAX shouldn't be an issue here, AFAICT.

Best regards,

You too,