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.
Jamal S. (Guest)
on 2007-02-17 14: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
Rimantas L. (Guest)
on 2007-02-17 14: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/
Jamal S. (Guest)
on 2007-02-17 14:28
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
unknown (Guest)
on 2007-02-17 14:31
(Received via mailing list)
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)
Jamal S. (Guest)
on 2007-02-17 15:54
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 :)

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