Redirecting after logging in

Hey everybody,

I'm trying to make an example of a small networking site, and when I log in a user, it should redirect and show his profile, but I get the following error:

NoMethodError in User#index

Showing app/views/user/index.html.erb where line #5 raised:

undefined method `screen_name' for nil:NilClass Extracted source (around line #5):

2: Your basic information 3: </h2> 4: <ul> 5: <li>Screen name: <%= @user.screen_name %></li> 6: <li>Email: <%= @user.email %></li> 7: <li>Password: ********** </li> 8: </ul>

Thanks a lot to everybody!

It means you haven't set @user to anything (or inadvertently set it to nil); look at your controller code.

Hassan Schroeder wrote:

I'm trying to make an example of a small networking site, and when I log in a user, it should redirect and show his profile, but I get the following error:

undefined method `screen_name' for nil:NilClass Extracted source (around line #5):

5: � <li>Screen name: <%= @user.screen_name %></li>

It means you haven't set @user to anything (or inadvertently set it to nil); look at your controller code.

-- Hassan Schroeder ------------------------ hassan.schroeder@gmail.com twitter: @hassan

Well, this is my code:

def login     @title = "Log in to RailsSpace"     if request.get?       @user = User.new(:remember_me => cookies[:remember_me] || "0")     elsif params_posted?(:user)       @user = User.new(params[:user])       user = User.find_by_screen_name_and_password(@user.screen_name,@user.password)       if user         user.login!(session)         if @user.remember_me? ? user.remember!(cookies) : user.forget!(cookies)           flash[:notice] = "User #{user.screen_name} logged in!"           redirect_to_forwarding_url         else           @user.clear_password!           flash[:notice] = "Invalid screen name/password combination"         end       end     end   end

private def redirect_to_forwarding_url     if (request_url = session[:protected_page])       session[:protected_page] = nil       redirect_to request_url     else       redirect_to :action => "index"     end   end

Where is the method for `index` ?

Hassan Schroeder wrote:

Uh, OK. See any "@user" there ? :slight_smile:

Hassan Schroeder wrote:

Hehe, ok, I just wrote @user = User.find_by_screen_name_and_email(@user.screen_name,@user.email)

That seems a little circular -- you're going to assign @user by using attributes of @user ?? :slight_smile:

but now, when I click the login button in order to log in my user, I don't get redirected

I'd suggest checking your log file.

Hassan Schroeder wrote:

If you can't work out what is happening to the flow in your code it can often be useful to use ruby-debug to break in to your code, where you can inspect data and step through the code. See the Rails Guide on debugging.

Colin

Colin Law wrote:

@user = User.find(session[:user_id]), thanks a lot :slight_smile: The second one, I'm afraid that will need a little more research because I can't find anything in the development.log

If you can't work out what is happening to the flow in your code it can often be useful to use ruby-debug to break in to your code, where you can inspect data and step through the code. See the Rails Guide on debugging.

Colin

Well, I tried, but I get lots of errors, even with ruby-debug19 -as I am using Ruby 1.9.1-p249- but I keep getting errors and that prevent me being able to use it:

Microsoft Windows [Version 6.1.7600] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\>gem install ruby-debug19 Building native extensions. This could take a while... ERROR: Error installing ruby-debug19:         ERROR: Failed to build gem native extension.

C:/Ruby191/bin/ruby.exe extconf.rb checking for vm_core.h... *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

Provided configuration options:         --with-opt-dir         --without-opt-dir         --with-opt-include         --without-opt-include=${opt-dir}/include         --with-opt-lib         --without-opt-lib=${opt-dir}/lib         --with-make-prog         --without-make-prog         --srcdir=.         --curdir         --ruby=C:/Ruby191/bin/ruby         --with-ruby-dir         --without-ruby-dir         --with-ruby-include         --without-ruby-include=${ruby-dir}/include         --with-ruby-lib         --without-ruby-lib=${ruby-dir}/lib C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:364:in `try_do': The complier failed to genera te an executable file. (RuntimeError) You have to install development tools first.         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:433:in `try_cpp'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:811:in `block in have_header'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:670:in `block in checking_for'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:276:in `block (2 levels) in postp one'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:250:in `open'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:276:in `block in postpone'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:250:in `open'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:272:in `postpone'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:669:in `checking_for'         from C:/Ruby191/lib/ruby/1.9.1/mkmf.rb:810:in `have_header'         from extconf.rb:15:in `block in <main>'         from C:/Ruby191/lib/ruby/gems/1.9.1/gems/ruby_core_source-0.1.4/lib/ruby _core_source.rb:18:in `call'         from C:/Ruby191/lib/ruby/gems/1.9.1/gems/ruby_core_source-0.1.4/lib/ruby _core_source.rb:18:in `create_makefile_with_core'         from extconf.rb:20:in `<main>'

Gem files will remain installed in C:/Ruby191/lib/ruby/gems/1.9.1/gems/linecache 19-0.5.11 for inspection. Results logged to C:/Ruby191/lib/ruby/gems/1.9.1/gems/linecache19-0.5.11/ext/tra ce_nums/gem_make.out

C:\>