# NoMethodError in UserController#login

**URL:** <https://www.ruby-forum.com/t/nomethoderror-in-usercontroller-login/143040>\
**Category:** Rails\
**Created:** [July 14, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-usercontroller-login/143040 "2008-07-14T18:59:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [July 14, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-usercontroller-login/143040/1 "2008-07-14T18:59:49Z")

</div>

this is the bit of code i’m having a problem with… the odd thing… if  
i remove all the code in the def login, reload the page so the view  
actually shows up put the code back in the def login and submit the  
form… it works just fine… but if i start with the code for the  
login in the controller then it’s broken and starts giving me the errors  
posted below.

def login

```
if session[:user] = User.authenticate(params[:user][:login],

```

params[:user][:password])  
redirect\_to :controller =\> ‘user’, :action =\> ‘pist’  
else  
redirect\_to :controller =\> ‘user’, :action =\> ‘mane’  
end

end

because it is producing this.

NoMethodError in UserController#login

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

RAILS\_ROOT: ./script/…/config/…  
Application Trace | Framework Trace | Full Trace

/Users/morgan/Sites/born/app/controllers/user\_controller.rb:21:in  
`login’

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_controller/base.rb:1101:in  
`send' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/base.rb:1101:in`perform\_action\_without\_filters’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_controller/filters.rb:696:in  
`call_filters' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/filters.rb:688:in`perform\_action\_without\_benchmark’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:293:in`measure’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_controller/benchmarking.rb:66:in  
`perform_action_without_rescue' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/rescue.rb:83:in`perform\_action’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_controller/base.rb:435:in  
`send' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/base.rb:435:in`process\_without\_filters’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_controller/filters.rb:684:in  
`process_without_session_management_support' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action_controller/session_management.rb:114:in`process’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/actionpack-1.13.6/lib/action\_controller/base.rb:334:in  
`process' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/dispatcher.rb:41:in`dispatch’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:76:in  
`process' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:74:in`synchronize’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/rails.rb:74:in  
`process' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:159:in`process\_client’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in  
`each' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:158:in`process\_client’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in  
`run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in`initialize’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in  
`new' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:285:in`run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in  
`initialize' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in`new’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel.rb:268:in  
`run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:282:in`run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:281:in  
`each' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/configurator.rb:281:in`run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel\_rails:128:in  
`run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/lib/mongrel/command.rb:212:in`run’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/mongrel-1.1.4/bin/mongrel\_rails:281  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/dependencies.rb:488:in  
`load' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:488:in`load’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/dependencies.rb:342:in  
`new_constants_in' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:488:in`load’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/servers/mongrel.rb:60  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom\_require.rb:27:in  
`gem_original_require' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in`require’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/dependencies.rb:495:in  
`require' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active_support/dependencies.rb:342:in`new\_constants\_in’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/activesupport-1.4.4/lib/active\_support/dependencies.rb:495:in  
`require' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rails-1.2.6/lib/commands/server.rb:39 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in`gem\_original\_require’  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rubygems/custom\_require.rb:27:in  
`require’  
script/server:3

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [July 14, 2008, 7:11pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-usercontroller-login/143040/2 "2008-07-14T19:11:13Z")

</div>

On Jul 14, 5:59 pm, Morgan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> this is the bit of code i’m having a problem with… the odd thing… if  
> i remove all the code in the def login, reload the page so the view  
> actually shows up put the code back in the def login and submit the  
> form… it works just fine… but if i start with the code for the  
> login in the controller then it’s broken and starts giving me the errors  
> posted below.

Assuming that you have a single login action that both displays the  
form and submits it then the problem is that if there are no  
parameters then params[:user] will be nil and so params[:user][:login]  
is nonsensical. When you submit the form however the parameters will  
be there and so there’s no problem.  
One way to deal with this would be to surround the relevant parts with  
if request.post? …

Fred

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [July 14, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-usercontroller-login/143040/3 "2008-07-14T19:28:13Z")

</div>

Frederick C. wrote:

> On Jul 14, 5:59ï¿½pm, Morgan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > this is the bit of code i’m having a problem with… ï¿½the odd thing… ï¿½if  
> > i remove all the code in the def login, ï¿½reload the page so the view  
> > actually shows up put the code back in the def login and submit the  
> > form… ï¿½it works just fine… ï¿½ but if i start with the code for the  
> > login in the controller then it’s broken and starts giving me the errors  
> > posted below.
> 
> Assuming that you have a single login action that both displays the  
> form and submits it then the problem is that if there are no  
> parameters then params[:user] will be nil and so params[:user][:login]  
> is nonsensical. When you submit the form however the parameters will  
> be there and so there’s no problem.  
> One way to deal with this would be to surround the relevant parts with  
> if request.post? …
> 
> Fred

i thought that made sense… i just didn’t know how to do it… thank  
you… FIXED.

---

<div class="post-metadata">

**Author:** ![morgan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/0ea827/32.png) [@morgan](https://www.ruby-forum.com/u/morgan)\
**Post date:** [July 14, 2008, 7:23pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-usercontroller-login/143040/4 "2008-07-14T19:23:53Z")

</div>

> ```
> if session[:user] = User.authenticate(params[:user][:login], params[:user][:password])
> 
> ```

What is this line of code supposed to do? The syntax seems to me  
completely wrong, hence Rails blowing on you.
