简体   繁体   中英

Ruby on Rails Tutorial Lesson 9

Update Saturday April 23, 2011

Lesson is green after implementing the following in user.rb

def authenticate_with_salt(id, cookie_salt)
  user = find_by_id(id)
  (user && user.salt == cookie_salt) ? user : nil 
end

I get:

Failures:

  1) SessionsController POST 'create' success should sign the user in
     Failure/Error: controller.current_user.should == @user
     ArgumentError:
       wrong number of arguments (1 for 2)
     # ./app/models/user.rb:45:in `authenticate_with_salt'
     # ./app/helpers/sessions_helper.rb:36:in `user_from_remember_token'
     # ./app/helpers/sessions_helper.rb:14:in `current_user'
     # ./spec/controllers/sessions_controller_spec.rb:58:in `block (4 levels) in <top (required)>'

Finished in 0.54296 seconds
7 examples, 1 failure

I am continuing to investigate... oh the joy! :D

EDIT: Working on finishing lesson 9 then will post back here -- seems that im back onto the path finding the problem myself again thanks to GrahamJRoy!

Update Friday April 22, 2011 :

I've spent the last day trying to find the similar problem but have been sorely unsuccessful, if you need any more information please let me know, both failures are highly likely related to each other so if i fix one i think it will fix the other

Failures:

  1) SessionsController POST 'create' success should sign the user in
     Failure/Error: post :create, :session => @attr
     NameError:
       undefined local variable or method `clear_return_to' for #<SessionsController:0x00000101648ac8>
     # ./app/helpers/sessions_helper.rb:28:in `redirect_back_or'
     # ./app/controllers/sessions_controller.rb:16:in `create'
     # ./spec/controllers/sessions_controller_spec.rb:51:in `block (4 levels) in <top (required)>'

  2) SessionsController POST 'create' success should redirect to the user show page
     Failure/Error: post :create, :session => @attr
     NameError:
       undefined local variable or method `clear_return_to' for #<SessionsController:0x00000100ea0690>
     # ./app/helpers/sessions_helper.rb:28:in `redirect_back_or'
     # ./app/controllers/sessions_controller.rb:16:in `create'
     # ./spec/controllers/sessions_controller_spec.rb:57:in `block (4 levels) in <top (required)>'

Finished in 0.42858 seconds
7 examples, 2 failures

In sessions helper.rb file:

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    clear_return_to
  end

In sessions_controller_spec.rb file:

  it "should sign the user in" do
     post :create, :session => @attr
     controller.current_user.should == @user
     controller.should be_signed_in
   end

  it "should redirect to the user show page" do
     post :create, :session => @attr
     response.should redirect_to(user_path(@user))
   end


In sessions_controller.rb file:

  def create
    user = User.authenticate(params[:session][:email],
                             params[:session][:password])
    if user.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Sign in"
      render 'new'
    else
      sign_in user
      redirect_back_or user
    end
  end

Original Problem below:


I'm currently working in Lesson 9 on the Ruby on Rails Tutorial and am getting the following error when running autotest:

Failures:

  1) SessionsController POST 'create' success should sign the user in
     Failure/Error: post :create, :session => @attr
     NoMethodError:
       undefined method `sign_in' for #<SessionsController:0x000001017082d8>
     # ./app/controllers/sessions_controller.rb:15:in `create'
     # ./spec/controllers/sessions_controller_spec.rb:51:in `block (4 levels) in <top (required)>'

  2) SessionsController POST 'create' success should redirect to the user show page
     Failure/Error: post :create, :session => @attr
     NoMethodError:
       undefined method `sign_in' for #<SessionsController:0x00000100ecbca0>
     # ./app/controllers/sessions_controller.rb:15:in `create'
     # ./spec/controllers/sessions_controller_spec.rb:57:in `block (4 levels) in <top (required)>'

sessions_controller_spec.rb:

  it "should sign the user in" do
    post :create, :session => @attr
    controller.current_user.should == @user
    controller.should be_signed_in
  end

  it "should redirect to the user show page" do
    post :create, :session => @attr
    response.should redirect_to(user_path(@user))
  end

IF i comment out the above sessions_controller_spec.rb I will be GREEN! Possibly that will help someone guide me in the right direction as I am clueless!

sessions_controller.rb:

def create
    user = User.authenticate(params[:session][:email],
                             params[:session][:password])
    if user.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Sign in"
      render 'new'
    else
      sign_in user
      redirect_back_or user
    end
  end

sessions_helper.rb:

def sign_in(user)
  cookies.permanent.signed[:remember_token] = [user.id, user.salt]
  self.current_user = user
end

ADDED per GrahamJRoy 'include SessionsHelper' in application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper
end

2 new errors that I think are leading to my problem:

Failures:

  1) SessionsController POST 'create' success should sign the user in
     Failure/Error: post :create, :session => @attr
     NoMethodError:
       undefined method `redirect_back_or' for #<SessionsController:0x0000010405adc0>
     # ./app/controllers/sessions_controller.rb:16:in `create'
     # ./spec/controllers/sessions_controller_spec.rb:51:in `block (4 levels) in <top (required)>'

  2) SessionsController POST 'create' success should redirect to the user show page
     Failure/Error: post :create, :session => @attr
     NoMethodError:
       undefined method `redirect_back_or' for #<SessionsController:0x000001030a3c60>
     # ./app/controllers/sessions_controller.rb:16:in `create'
     # ./spec/controllers/sessions_controller_spec.rb:57:in `block (4 levels) in <top (required)>'

Per the documentation, have you referenced the SessionsHelper in the ApplicationController?

class ApplicationController < ActionController::Base
  protect_from_forgery
  include SessionsHelper
end

So I basically backtracked and started the lesson over again and carefully commented out the code on each page that shouldnt be there.

My Saturday update above made me start inspecting what the failure was showing, so I went to each line it mentioned and checked RailsTutorial git repo to see any obvious differences.

This led to the * next to the remember_token

User.authenticate_with_salt(*remember_token)

When adding that * the error changed from

Failures:

  1) SessionsController POST 'create' success should sign the user in
     Failure/Error: controller.current_user.should == @user
     ArgumentError:
       wrong number of arguments (1 for 2)
     # ./app/models/user.rb:45:in `authenticate_with_salt'
     # ./app/helpers/sessions_helper.rb:36:in `user_from_remember_token'
     # ./app/helpers/sessions_helper.rb:14:in `current_user'
     # ./spec/controllers/sessions_controller_spec.rb:58:in `block (4 levels) in <top (required)>'

Finished in 0.54296 seconds
7 examples, 1 failure

to:

Failures:

  1) SessionsController POST 'create' success should sign the user in
     Failure/Error: controller.should be_signed_in
     NoMethodError:
       undefined method `signed_in?' for #<SessionsController:0x00000104086088>
     # ./spec/controllers/sessions_controller_spec.rb:59:in `block (4 levels) in <top (required)>'

Finished in 1.73 seconds
63 examples, 1 failure

At which point i checked my code:

  def signed_in
    !current_user.nil?
  end

to the git repoos:

  def signed_in?
    !current_user.nil?
  end

And conveniently saw a '?' missing which I figured I would add and see what happens..

And apparently I'm green now... lets finish this lesson now!

Many thanks to GrahamJRoy for your input!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM