Hey all, I have this in my plugins directory: 1: <% if current_user && current_user.admin? %> 2: <div class="blogAdminLinks"> 3: <ul class="blogList"> 4: <li class="blogListFirst"><%= link_to ''New Blog Post'', new_blog_post_path %></li> As you can see,it references current_user So I define current_user in User model as a global variable: cattr_accessor :current_user I application controller, I define who current user is: def current_user @current_user = User.find(1) return @current_user if defined?(@current_user) end It does appear that the method overrides the one in model, assuming the one in model is a getter/setter. Nevertheless, when I make a reference to it in one of my views, I get undefined error method. Any idea? Thanks for response. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Feb 22, 2011 at 1:18 PM, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hey all, > > I have this in my plugins directory: > > 1: <% if current_user && current_user.admin? %> > 2: <div class="blogAdminLinks"> > 3: <ul class="blogList"> > 4: <li class="blogListFirst"><%= link_to ''New Blog Post'', > new_blog_post_path %></li> > > As you can see,it references current_user > > So I define current_user in User model as a global variable: > > cattr_accessor :current_user > > I application controller, I define who current user is: > > def current_user > @current_user = User.find(1) > > return @current_user if defined?(@current_user) > end > > It does appear that the method overrides the one in model, assuming the > one in model is a getter/setter. > > Nevertheless, when I make a reference to it in one of my views, I get > undefined error method. Any idea? >At the top of your controller, you''ll want to add helper_method :current_user But I don''t understand what the ''cattr_accessor :current_user'' in your user model accomplishes; where or how will you use that? You aren''t using it in the controller. What I''m saying is, you don''t need that, and probably don''t want it. :-)> > Thanks for response. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> At the top of your controller, you''ll want to add > helper_method :current_userThanks for response, but I place this in application controller: helper_method :current_user private def require_login unless logged_in? flash[:error] = "You must be logged in to access this section" redirect_to login_path end end def logged_in? !!current_user end def current_user @current_user ||= session[:user_id] && User.find(session[:user_id]) end And now because all my contorllers inherit from it, I get undefined method current_user on all the pages, even though current user is clearly defined. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 23 February 2011 15:26, John Merlino <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Thanks for response, but I place this in application controller: > > helper_method :current_user > > private > > def current_user > @current_user ||= session[:user_id] && User.find(session[:user_id]) > end > > > And now because all my contorllers inherit from it, I get undefined > method current_user on all the pages, even though current user is > clearly defined.Clearly defined, but private. Whip a "public" in front of it, or move the method above the private declaration. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.