2011-04-20 123 views
0

更新星期六2011年4月23日Ruby on Rails的教程第9課

課是實現後user.rb以下綠色

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

我得到:

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 

我繼續調查......哦,歡樂! :d

編輯:在完成第9課,然後將回到這裏後工作 - 似乎,我回來到路徑上找到自己的問題再次感謝GrahamJRoy!

更新星期五2011年4月22日:

我花了最後一天試圖找到類似的問題,但一直沒有引起人們不成功的,如果您需要任何更多信息,請讓我知道,無論是失敗極有可能彼此相關的,如果我解決一個我認爲這將解決其他

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 

在會議helper.rb文件:

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

在sessions_contro ller_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 


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 

原題如下:


我目前在第9課工作的Ruby on Rails的教程和運行自動測試時,我收到以下錯誤:

失敗:

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 

如果我註釋掉上述sessions_controller_spec.rb我會變綠!可能這會幫助某人指引我正確的方向,因爲我很無聊!

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 

每GrahamJRoy增加了 '包括SessionsHelper' 在application_controller.rb

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    include SessionsHelper 
end 

2個新的錯誤,我認爲導致我的問題:

失敗:

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)>' 

回答

5

每文檔,有你在ApplicationController中引用的SessionsHelper?

class ApplicationController < ActionController::Base 
    protect_from_forgery 
    include SessionsHelper 
end 
+0

嗯都能跟得上我想補充說的application_controller.rb文件,並將其殺死自動測試:「SAMPLE_APP:無法運行測試」 – rzschau 2011-04-20 21:17:00

+1

應包括SessionsHelper(帶S) - 現在2個不同的錯誤 – rzschau 2011-04-20 21:29:15

+0

遺憾的錯字。現在聽起來您在sessions_helper中缺少以下方法:def redirect_back_or(默認)請參閱清單10.17 – GrahamJRoy 2011-04-21 12:01:39

0

所以我基本上是退步,開始教訓一遍,仔細註釋掉每個頁面,不應該是那裏的代碼。

我上週六的更新讓我開始檢查什麼是失敗顯示,所以我去了它提到的每一行,並檢查RailsTutorial git回購看到任何明顯的差異。

這導致*旁邊remember_token

User.authenticate_with_salt(*remember_token) 

當並稱*從

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 

改爲錯誤:

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 

在這一點上,我檢查了我代碼:

def signed_in 
    !current_user.nil? 
    end 

到GIT repoos:

def signed_in? 
    !current_user.nil? 
    end 

,方便地看到一個 '?'錯過了,我想我會添加,看看會發生什麼..

而且顯然我現在綠色...現在讓我們完成這一課!

非常感謝GrahamJRoy的輸入!