Question concerning controllers and a global layout

Hi everybody,

I have been successfully playing around with rails so far, but now I
have something that I haven’t encountered so far. :slight_smile:

I am working on an application where users can login to, search for
stored data (newsagency data in this case) and export it to certain
target systems or “folders” in the system.

I created a nice rails app, I have several controllers to handle the
seperate parts of the application - one for doing the search, one for
enquiries, one to manage the folders, etc. pp.

But now I have one page element, that has to be visible on almost every
page, and this element is user dependent. I like to show the user the
available exports channels and folders, that means I have to render user
dependent date into a globally available page area. Partial come here to
my mind.

How do I solve this and where do I locate this page component, which has
from my perspective be defined in the global layout, but has to be
filled based on data relevant to the current user?

Any hints are highly appreciated.

Best regards,
Oliver

On 2/11/07, Oliver A. [email protected] wrote:

I created a nice rails app, I have several controllers to handle the
from my perspective be defined in the global layout, but has to be
filled based on data relevant to the current user?

Any hints are highly appreciated.

Best regards,
Oliver

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., @user.name to get the user name.

Regards,
George.

Hi George,

George O. wrote:

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., @user.name to get the user name.

Okay, then I go that way:

  • 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?

Best regards,
Oliver

On 2/11/07, Oliver A. [email protected] 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.

Good luck!
George.

George O. wrote:

On 2/11/07, Oliver A. [email protected] 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:

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

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, …

Best regards,
Oliver

On 2/11/07, Oliver A. [email protected] wrote:

‘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
private
def setup_user
@user = User.model
end
before_filter :setup_user
end

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,
Oliver

You too,
George.