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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Matthew Isleb
2006-Oct-09 17:12 UTC
Re: Problem setting variables in ApplicationController.
Larry Kelly 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jason Norris
2006-Oct-09 17:14 UTC
Re: Problem setting variables in ApplicationController.
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 Larry Kelly 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. > > Is there a better way of building dynamic menus?? > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Matthew Isleb
2006-Oct-09 17:29 UTC
Re: Problem setting variables in ApplicationController.
Jason Norris 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jason Norris
2006-Oct-09 18:27 UTC
Re: Problem setting variables in ApplicationController.
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 Matthew Isleb wrote:> Jason Norris 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 > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Matthew Isleb
2006-Oct-09 18:39 UTC
Re: Problem setting variables in ApplicationController.
Jason Norris 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---