# Problem with nil.user

**URL:** <https://www.ruby-forum.com/t/problem-with-nil-user/166821>\
**Category:** Rails\
**Created:** [April 29, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821 "2009-04-29T22:15:15Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 29, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/1 "2009-04-29T22:15:15Z")

</div>

Hi all, I’ve put new code into my app so that an administrator will have  
extra privileges (administer users, edit pages). The error is:

‘Couldn’t find User without an ID’

So the app is looking for a logged in user when the homepage is  
accessed.

Heres my code:

site/index view:

\<% if is\_logged\_in? and logged\_in\_user.has\_role?(‘Moderator’) -%\>  
\<%= link\_to ‘Administer Users’, :controller =\> ‘user’, :action =\>  
‘show’ %\>  
\<%= link\_to ’ | Edit pages’, pages\_path %\>  
\<% end %\>

application helper:

def is\_logged\_in?  
@logged\_in\_user = User.find(session[:user\_id])  
@logged\_in\_user ? @logged\_in\_user : false  
end

def logged\_in\_user  
return @logged\_in\_user if is\_logged\_in?

How could I change the code to rectify this? Any help would be greatly  
appreciated

Steve.

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 29, 2009, 11:10pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/2 "2009-04-29T23:10:03Z")

</div>

Rick Lloyd wrote:

> Try
> 
> def is\_logged\_in?  
> @logged\_in\_user = User.find(session[:user]) if session[:user]  
> end
> 
> On Apr 29, 10:15ï¿½am, Stephen F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks Rick. It worked a treat and also opened up a couple of gaping  
holes aswell (which I’ve managed to rectify!)

Cheers

Stephen.

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 29, 2009, 10:30pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/3 "2009-04-29T22:30:43Z")

</div>

Try

def is\_logged\_in?  
@logged\_in\_user = User.find(session[:user]) if session[:user]  
end

On Apr 29, 10:15 am, Stephen F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 29, 2009, 11:27pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/4 "2009-04-29T23:27:01Z")

</div>

Just to followup if you’re interested, … the error msg from the op  
was due to the fact that the call to session[:user\_id] returned nil,  
and thus calling find(nil) resulted in that error being raised. If  
you wanted to avoid such an error and just have the find call return  
nil if not found, one way is to just call find\_by\_id:

$ ./script/console

> > u = User.find(nil)  
> > ActiveRecord::RecordNotFound: Couldn’t find User without an ID  
> > from /usr/lib/ruby/…  
> > …

> > u = User.find\_by\_id(nil)  
> > =\> nil

Jeff

On Apr 29, 2:10 pm, Stephen F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 30, 2009, 9:09pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/5 "2009-04-30T21:09:07Z")

</div>

E. Litwin wrote:

> When are you saving the user\_id to the session?
> 
> You need to force a redirect to a login page if there is no session  
> [:user\_id] and you should also handle the fact that session[:user\_id]  
> may be nil in your is\_logged\_in? method.

Its saved in the login method:

user = User.find\_by\_user\_name(@user.user\_name)

```
  if user and user.password_matches?(@user.password)
    user.login!(session)
    if @user.remember_me == "1"
      cookies[:remember_me] = { :value => "1",
                                :expires => 10.years.from_now }
      user.authorization_token = Digest::SHA1.hexdigest(
                                   "#{user.user_name}:#{user.password}")

```

I have a redirect if someone tries to access the admin privileges if  
thats what you mean?

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 29, 2009, 11:51pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/6 "2009-04-29T23:51:36Z")

</div>

When are you saving the user\_id to the session?

You need to force a redirect to a login page if there is no session  
[:user\_id] and you should also handle the fact that session[:user\_id]  
may be nil in your is\_logged\_in? method.

On Apr 29, 1:15 pm, Stephen F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [April 30, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/7 "2009-04-30T22:52:07Z")

</div>

Just to clarify - are you setting session[:user\_id] in the user.login!  
(session) call?  
The other suggestions on handling a nil session[:user\_id] (i.e. using  
find\_by\_id) are the appropriate ways to handle the error.

Eric

On Apr 30, 12:09 pm, Stephen F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![furrot](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/3da27b/32.png) [@furrot](https://www.ruby-forum.com/u/furrot)\
**Post date:** [May 1, 2009, 6:11pm UTC](https://www.ruby-forum.com/t/problem-with-nil-user/166821/8 "2009-05-01T18:11:46Z")

</div>

E. Litwin wrote:

> Just to clarify - are you setting session[:user\_id] in the user.login!  
> (session) call?  
> The other suggestions on handling a nil session[:user\_id] (i.e. using  
> find\_by\_id) are the appropriate ways to handle the error.
> 
> Eric
> 
> On Apr 30, 12:09ï¿½pm, Stephen F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Yes,

#log a user in  
def login!(session)  
session[:user\_id] = self.id  
end

Steve
