# Question concerning controllers and a global layout

**URL:** <https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205>\
**Category:** Rails\
**Created:** [February 11, 2007, 11:02am UTC](https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205 "2007-02-11T11:02:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [February 11, 2007, 11:02am UTC](https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205/1 "2007-02-11T11:02:57Z")

</div>

Hi everybody,

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

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

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [February 11, 2007, 11:13am UTC](https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205/2 "2007-02-11T11:13:56Z")

</div>

On 2/11/07, Oliver A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [February 11, 2007, 11:16am UTC](https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205/3 "2007-02-11T11:16:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [February 11, 2007, 11:29am UTC](https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205/4 "2007-02-11T11:29:59Z")

</div>

On 2/11/07, Oliver A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [February 11, 2007, 12:34pm UTC](https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205/5 "2007-02-11T12:34:51Z")

</div>

George O. wrote:

> On 2/11/07, Oliver A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

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

---

<div class="post-metadata">

**Author:** ![oandrich](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/3da27b/32.png) [@oandrich](https://www.ruby-forum.com/u/oandrich)\
**Post date:** [February 12, 2007, 12:41pm UTC](https://www.ruby-forum.com/t/question-concerning-controllers-and-a-global-layout/85205/6 "2007-02-12T12:41:49Z")

</div>

On 2/11/07, Oliver A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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. 🙂

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.
