Forum: Ruby on Rails every step error? why that?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-02-17 13:06
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
Ec5a599777854c540fd102ef4691fe10?d=identicon&s=25 Rimantas Liubertas (Guest)
on 2007-02-17 13:13
(Received via mailing list)
>      @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/
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-02-17 13:28
Rimantas Liubertas 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2007-02-17 13:31
(Received via mailing list)
Hi --

On Sat, 17 Feb 2007, Jamal Soueidan 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)
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-02-17 14:54
unknown wrote:
> Hi --
>
> On Sat, 17 Feb 2007, Jamal Soueidan 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 :)

Thanks for your help :D
This topic is locked and can not be replied to.