Every step error? why that?

Hi,

Its like every time I try to do something I get a error, and every time
I search on the Internet I find things more complicated? The thing I’m
doing is very simple.

 @user = User.find(:first, :conditions => ["username = ?",

params[:user][:username]] )
if @user.username?
redirect_to :action => ‘account’
end

This should be simple login system, I want to check if the username
exists in the table? But this give me NIL exception?

//Jamal

 @user = User.find(:first, :conditions => ["username = ?",

params[:user][:username]] )
if @user.username?
redirect_to :action => ‘account’
end

This should be simple login system, I want to check if the username
exists in the table? But this give me NIL exception?

Suppose username you are looking for does not exist.
User.find will return nil. Next you call username on nil… oops.

Try @user instead of @user.username

Regards,
Rimantas

http://rimantas.com/

Rimantas L. wrote:

 @user = User.find(:first, :conditions => ["username = ?",

params[:user][:username]] )
if @user.username?
redirect_to :action => ‘account’
end

This should be simple login system, I want to check if the username
exists in the table? But this give me NIL exception?

Suppose username you are looking for does not exist.
User.find will return nil. Next you call username on nil… oops.

Try @user instead of @user.username

Regards,
Rimantas

http://rimantas.com/

Thats works perfect, but could I do this?

if @user.username != nil

Hi –

On Sat, 17 Feb 2007, Jamal S. wrote:

Thats works perfect, but could I do this?

if @user.username != nil

No, because the problem is that @user itself might be nil. If it is,
you don’t want to call username on it at all.

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

unknown wrote:

Hi –

On Sat, 17 Feb 2007, Jamal S. wrote:

Thats works perfect, but could I do this?

if @user.username != nil

No, because the problem is that @user itself might be nil. If it is,
you don’t want to call username on it at all.

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

True :slight_smile:

Thanks for your help :smiley: