# NoMethodError application\_controller.rb:10

**URL:** <https://www.ruby-forum.com/t/nomethoderror-application-controller-rb-10/177666>\
**Category:** Rails\
**Created:** [October 21, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/nomethoderror-application-controller-rb-10/177666 "2009-10-21T18:51:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtacconi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rtacconi](https://www.ruby-forum.com/u/rtacconi)\
**Post date:** [October 21, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/nomethoderror-application-controller-rb-10/177666/1 "2009-10-21T18:51:56Z")

</div>

When I try to set a session variable, in ApplicationController, I get  
this:

NoMethodError in ManagementController#index

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]=

RAILS\_ROOT: c:/dev/qna  
Application Trace | Framework Trace | Full Trace

c:/dev/qna/app/controllers/application\_controller.rb:10  
c:/dev/qna/app/controllers/management\_controller.rb:1

The code is below

class ApplicationController \< ActionController::Base  
helper :all # include all helpers, all the time  
protect\_from\_forgery # See  
ActionController::RequestForgeryProtection for details  
session[:current\_user\_id] = “546”  
end

Why I cannot set the session varaible?

---

<div class="post-metadata">

**Author:** ![rtacconi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rtacconi](https://www.ruby-forum.com/u/rtacconi)\
**Post date:** [October 21, 2009, 7:14pm UTC](https://www.ruby-forum.com/t/nomethoderror-application-controller-rb-10/177666/2 "2009-10-21T19:14:16Z")

</div>

rtacconi wrote:

> When I try to set a session variable, in ApplicationController, I get  
> this:
> 
> NoMethodError in ManagementController#index
> 
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of ActiveRecord::Base.  
> The error occurred while evaluating nil.[]=
> 
> RAILS\_ROOT: c:/dev/qna  
> Application Trace | Framework Trace | Full Trace
> 
> c:/dev/qna/app/controllers/application\_controller.rb:10  
> c:/dev/qna/app/controllers/management\_controller.rb:1
> 
> The code is below
> 
> class ApplicationController \< ActionController::Base  
> helper :all # include all helpers, all the time  
> protect\_from\_forgery # See  
> ActionController::RequestForgeryProtection for details  
> session[:current\_user\_id] = “546”  
> end

Really? That’s only about 5 lines of code and the error is being  
reported on line 10. What’s on line 10?

> Why I cannot set the session varaible?

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![rtacconi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/13edae/32.png) [@rtacconi](https://www.ruby-forum.com/u/rtacconi)\
**Post date:** [October 21, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/nomethoderror-application-controller-rb-10/177666/3 "2009-10-21T19:52:51Z")

</div>

On Oct 21, 5:49 pm, rtacconi [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Application Trace | Framework Trace | Full Trace  
> session[:current\_user\_id] = “546”  
> This is executing when application controller loads, in the context of  
> the class, (rather than an individual instance of it) which doesn’t  
> make any sense. If you want to play with the session you need to be  
> doing so from an instance method of a controller (since session itself  
> is an instance method of controllers)

Fred
