So confused -- logging in

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:insubclasses’
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:ineach’
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:insubclasses’
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:inres
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:indis
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:insynchronize’
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 :inprocess_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 :inprocess_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 :ininitialize’
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 :inrun’
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 :innew’
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:inrun’
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:inrun’
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:inrun’
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!

On Nov 10, 2007 1:40 PM, Stedwick [email protected] 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/

You should look at acts_as_authenticated. It takes care of all this
for you and is easy to extend.

-Bill

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

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.