Hey all,
I have this in my plugins directory:
1: <% if current_user && current_user.admin? %>
2:
3:
4: - <%= link_to ‘New Blog Post’,
new_blog_post_path %>
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.
On Tue, Feb 22, 2011 at 1:18 PM, John M. [email protected]
wrote:
As you can see,it references current_user
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. 
At the top of your controller, you’ll want to add
helper_method :current_user
Thanks 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.
On 23 February 2011 15:26, John M. [email protected] wrote:
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.