# problem with nil.user

**URL:** <https://discuss.rubyonrails.org/t/problem-with-nil-user/42078>\
**Category:** rubyonrails-talk\
**Created:** [April 29, 2009, 8:25pm UTC](https://discuss.rubyonrails.org/t/problem-with-nil-user/42078 "2009-04-29T20:25:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![E\_Litwin](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@E\_Litwin](https://discuss.rubyonrails.org/u/E_Litwin)\
**Post date:** [April 29, 2009, 8:25pm UTC](https://discuss.rubyonrails.org/t/problem-with-nil-user/42078/1 "2009-04-29T20:25:44Z")

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

---

<div class="post-metadata">

**Author:** ![E\_Litwin](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@E\_Litwin](https://discuss.rubyonrails.org/u/E_Litwin)\
**Post date:** [April 30, 2009, 8:51pm UTC](https://discuss.rubyonrails.org/t/problem-with-nil-user/42078/2 "2009-04-30T20:51:11Z")

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