# Problem setting variables in ApplicationController

**URL:** <https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522>\
**Category:** Rails\
**Created:** [October 9, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522 "2006-10-09T18:10:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [October 9, 2006, 6:10pm UTC](https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522/1 "2006-10-09T18:10:02Z")

</div>

I’m dynamically generating menu options based on the currently logged  
in user: current\_user.  
My menus are setup on the applicatin.rhtml file. The current\_user is  
defined in the application controller. But, attempting to assign it a  
value while in the application controller results in an ‘undefined  
method’ error.

ApplicationController …

## def current\_user @cu = ::User.find(session[:rbac\_user\_id]) || “not logged in” end

++Application.rhtml ++  
Admin menu  
\<% if current\_user.has\_role?(“Admin”) %\>  
menu one  
menu two  
\<%end%\>

Apparently the current\_user is not created by the time the  
application.rhtml is rendered.  
I think it might have something to do with the special nature of the  
ApplicationController. But, I don’t fully understand what is  
happening here.

Is there a better way of building dynamic menus??

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [October 9, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522/2 "2006-10-09T19:12:27Z")

</div>

Larry K. wrote:

> I’m dynamically generating menu options based on the currently logged  
> in user: current\_user.  
> My menus are setup on the applicatin.rhtml file. The current\_user is  
> defined in the application controller. But, attempting to assign it a  
> value while in the application controller results in an ‘undefined  
> method’ error.
> 
> ApplicationController …
> 
> ## def current\_user @cu = ::User.find(session[:rbac\_user\_id]) || “not logged in” end
> 
> ++Application.rhtml ++  
> Admin menu  
> \<% if current\_user.has\_role?(“Admin”) %\>  
> menu one  
> menu two  
> \<%end%\>
> 
> Apparently the current\_user is not created by the time the  
> application.rhtml is rendered.  
> I think it might have something to do with the special nature of the  
> ApplicationController. But, I don’t fully understand what is  
> happening here.

Methods defined in application.rb are not available in views (including  
the layout). If you want methods for use in views, define them in a  
helper. But that probably isn’t what you want here.

Keep this:

def current\_user  
@cu = ::User.find(session[:rbac\_user\_id]) || “not logged in”  
end

And add:

before\_filter :current\_user

at the top of application.rb. In the layout, reference  
@cu.has\_role(“Admin”)

Also, note that things will break when there is no user logged in. If  
you set @cu to a String, @cu.has\_role will cause an error. Try changing  
current\_user to:

def current\_user  
if @cu = ::User.find(session[:rbac\_user\_id])  
return true  
else  
return false  
end  
end

That “return false” will cause processing of the request to end. You can  
add a “redirect\_to” before “return false” if you’d like to do sometning  
more useful on failure.

-matthew

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [October 9, 2006, 7:22pm UTC](https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522/3 "2006-10-09T19:22:17Z")

</div>

This should be

\<% if @cu && @cu.has\_role?(“Admin”) %\>  
menu one  
menu two  
\<%end%\>

That way it makes sure there is a user, then checks their role.

You need to use instance variables to get data to your views. You can’t  
call a method in your controller from the view.  
Call current\_user in your function before the page is rendered.  
I’m working on a role-based project right now also, so if you find a  
better way I’d like to hear about it.

Jason

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [October 9, 2006, 7:29pm UTC](https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522/4 "2006-10-09T19:29:39Z")

</div>

Jason N. wrote:

> Call current\_user in your function before the page is rendered.  
> I’m working on a role-based project right now also, so if you find a  
> better way I’d like to hear about it.

before\_filters are your friend.

-matthew

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [October 9, 2006, 8:32pm UTC](https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522/5 "2006-10-09T20:32:28Z")

</div>

Definitely, for whole pages that are role-based. But for conditionally  
displaying parts of a menu, what’s the best route?  
Putting an if directly in the view is what I’ve been doing, but it seems  
that there should be a more elegant approach.

Jason

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [October 9, 2006, 8:39pm UTC](https://www.ruby-forum.com/t/problem-setting-variables-in-applicationcontroller/71522/6 "2006-10-09T20:39:39Z")

</div>

Jason N. wrote:

> Definitely, for whole pages that are role-based. But for conditionally  
> displaying parts of a menu, what’s the best route?  
> Putting an if directly in the view is what I’ve been doing, but it seems  
> that there should be a more elegant approach.

You can still use the before\_filter for generic (auth/noauth) pages.  
Just have it always return true. I think you’ll need to check “if @cu”  
in the view. Can’t think of any way around that.

-matthew
