# Undefined method current\_user issue

**URL:** <https://www.ruby-forum.com/t/undefined-method-current-user-issue/203550>\
**Category:** Rails\
**Created:** [February 22, 2011, 8:18pm UTC](https://www.ruby-forum.com/t/undefined-method-current-user-issue/203550 "2011-02-22T20:18:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [February 22, 2011, 8:18pm UTC](https://www.ruby-forum.com/t/undefined-method-current-user-issue/203550/1 "2011-02-22T20:18:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 23, 2011, 1:03am UTC](https://www.ruby-forum.com/t/undefined-method-current-user-issue/203550/2 "2011-02-23T01:03:22Z")

</div>

On Tue, Feb 22, 2011 at 1:18 PM, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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. 🙂

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 23, 2011, 4:26pm UTC](https://www.ruby-forum.com/t/undefined-method-current-user-issue/203550/3 "2011-02-23T16:26:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 23, 2011, 8:29pm UTC](https://www.ruby-forum.com/t/undefined-method-current-user-issue/203550/4 "2011-02-23T20:29:57Z")

</div>

On 23 February 2011 15:26, John M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.
