# So confused -- logging in

**URL:** <https://www.ruby-forum.com/t/so-confused-logging-in/119808>\
**Category:** Rails\
**Created:** [November 10, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/so-confused-logging-in/119808 "2007-11-10T19:42:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 10, 2007, 7:42pm UTC](https://www.ruby-forum.com/t/so-confused-logging-in/119808/1 "2007-11-10T19:42:29Z")

</div>

I’ve spent hours on this, and I can’t figure it out. I’m trying to  
create a simple login system. Here’s my accounts controller.

class AccountsController \< ApplicationController

# ssl\_required :login

def login  
if !(params[:username] and params[:password])  
flash[:error] = “Username or password cannot be blank.”  
redirect\_to :action =\> :index  
return  
end

```
username = params[:username]
hashed_password = "hello" #Account.hash(params[:password])

account = Account.find_by_username_and_hashed_password(username,

```

hashed\_password)

```
if account == nil
  flash[:error] = "Incorrect username or password."
  redirect_to :action => :index
  return
end

session[:user_id] = account.id
redirect_to :controller => :home

```

end

def logout  
session[:user\_id] = nil  
flash[:notice] = “You have logged out.”  
redirect\_to :controller =\> :home  
end  
end

The logout function works fine. The first part of the login function,  
the part that tests to make sure that a username and password are  
present, works fine as well. However, if I pass username and password  
parameters to the function (regardless if they correspond to a real  
account or not), everything blows up in my face. In fact, my whole  
application permanently crashes until I run “rake db:sessions:clear”.  
Even if I restart mongrel and Apache, my application still doesn’t  
work until the sessions are cleaned out. My Account.hash and  
Account.find\_by\_username\_and\_hashed\_password functions both work fine,  
and I tried turning them off. No luck.

Any ideas?

Oh, and the errors I get in the mongrel console are:

\*\* Starting Mongrel listening at 0.0.0.0:3000  
\*\* Starting Rails with development environment…  
\*\* Rails loaded.  
\*\* Loading any Rails specific GemPlugins  
\*\* Signals ready. INT =\> stop (no restart).  
\*\* Mongrel available at 0.0.0.0:3000  
\*\* Use CTRL-C to stop.  
Sat Nov 10 13:26:13 -0500 2007: Error calling Dispatcher.dispatch  
#\<ArgumentErro  
r: wrong number of arguments (0 for 1)\>  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/ba  
se.rb:1318:in `hash' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/ba se.rb:1318:in`[]’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/ba  
se.rb:1318:in `subclasses' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/ba se.rb:1319:in`subclasses’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/actionpack-1.13.5/lib/  
action\_controller/  
routing.rb:351:in `inject' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/ba se.rb:1319:in`each’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/ba  
se.rb:1319:in `inject' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/ active_record/ba se.rb:1319:in`subclasses’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/  
active\_record/ba  
se.rb:280:in `reset_subclasses' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb: 57:in`res  
et\_application!’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb:  
115:in `re set_after_dispatch' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/rails-1.2.5/lib/dispatcher.rb: 50:in`dis  
patch’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel/rails.  
rb:78:in `process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel/rails. rb:76:in`synchronize’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel/rails.  
rb:76:in `process' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel.rb:618 :in`process\_client’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel.rb:617  
:in `each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel.rb:617 :in`process\_client’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel.rb:736  
:in `run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel.rb:736 :in`initialize’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel.rb:736  
:in `new' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel.rb:736 :in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel.rb:720  
:in `initialize' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel.rb:720 :in`new’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel.rb:720  
:in `run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel/config urator.rb:271:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/  
mongrel/config  
urator.rb:270:in `each' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel/config urator.rb:270:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/  
mongrel\_rails:  
127:in `run' C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/lib/ mongrel/comman d.rb:211:in`run’  
C:/INSTAN~1/ruby/lib/ruby/gems/1.8/gems/mongrel-1.0.1-mswin32/bin/  
mongrel\_rails:  
243  
C:/INSTAN~1/ruby/bin/mongrel\_rails:16:in `load’  
C:/INSTAN~1/ruby/bin/mongrel\_rails:16

Thanks!

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 10, 2007, 7:54pm UTC](https://www.ruby-forum.com/t/so-confused-logging-in/119808/2 "2007-11-10T19:54:48Z")

</div>

On Nov 10, 2007 1:40 PM, Stedwick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> redirect_to :action => :index
> return
> end
> 
> username = params[:username]
> hashed_password = "hello" #Account.hash(params[:password])
> 
> ```

> #\<ArgumentErro  
> r: wrong number of arguments (0 for 1)\>

I’m guessing that you haven’t defined Account#hash

BUT DON"T DO THAT

Object#hash is a core ruby method which takes no arguments.

You should name the method something like Account#hash\_password to  
avoid conflicting with it.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 10, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/so-confused-logging-in/119808/3 "2007-11-10T21:08:39Z")

</div>

You should look at acts\_as\_authenticated. It takes care of all this  
for you and is easy to extend.

-Bill

---

<div class="post-metadata">

**Author:** ![stedwick](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90ced4/32.png) [@stedwick](https://www.ruby-forum.com/u/stedwick)\
**Post date:** [November 10, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/so-confused-logging-in/119808/4 "2007-11-10T21:38:11Z")

</div>

I’ve looked at “ask as authenticated”, but I’m trying to do it the  
hard way so I learn along the way 🙂

Interesting update: my accounts controller works on my web host, it  
just doesn’t work with instant rails. Rather annoying, but I guess the  
problem isn’t as serious as I first thought.

I’m thinking the problem might have something to do with this line:

Sat Nov 10 13:26:13 -0500 2007: Error calling Dispatcher.dispatch

On my web host, it’s set up to run as a CGI application, which I will  
eventually migrate to fast CGI, but instant rails uses mongrel. Do you  
think that could be the problem?

Is there an easy way I can fix this so that it runs on my home  
computer using instant rails? Because I like to do my development on  
my home computer… but now it looks like I’ll have to upload  
everything to my server and test it there.

On a side note, relating to “ask as authenticated”, is there a good  
plug-in that makes it easy to authenticate using Google accounts? I  
found a plug-in called “acts as Google accounts”, but the comments  
said it sucked.

And thanks Rick, I will change my “hash” function to another name.
