Problem setting variables in ApplicationController.

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"

This should be

<% if @cu && @cu.has_role?("Admin") %>
  menu one
  menu two

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.


Larry Kelly 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.


Matthew Isleb wrote: