2016-02-10 46 views
0

已編輯:現在發生的不同錯誤,發佈的代碼仍然相同。Rails教程(M. Hartl)第8章錯誤:未定義的本地變量

關於Hartl的Ruby教程第8.3章。在有效登錄測試的耙測試中獲取此錯誤。

test_login_with_valid_information_followed_by_logout#UsersLoginTest (1454541872.34s) 
NameError:   NameError: undefined local variable or method `redirect_to_root_url' for #<SessionsController:0x007fce97a93068> 
      app/controllers/sessions_controller.rb:18:in `destroy' 
      test/integration/users_login_test.rb:39:in `block in <class:UsersLoginTest>' 
     app/controllers/sessions_controller.rb:18:in `destroy' 
     test/integration/users_login_test.rb:39:in `block in <class:UsersLoginTest>' 

錯誤信息點在我的會話控制器錯誤排隊在我登錄測試和線18 39,你可以在這裏看到:

test/integration/users_login_test.rb:39:in `block in <class:UsersLoginTest>' 

app/controllers/sessions_controller.rb:18:in `destroy' 

users_login_test.rb:

39號線將 '刪除logout_path'

require 'test_helper' 

class UsersLoginTest < ActionDispatch::IntegrationTest 
    def setup 
     @user = users(:michael) 
    end 

    test "login with invalid information" do 
     get login_path 
     assert_template 'sessions/new' 
     post login_path, session: { email: "", password: "" } 
     assert_template 'sessions/new' 
     assert_not flash.empty? 
     get root_path 
     assert flash.empty? 
    end 

    test "login with valid information" do 
     get login_path 
     post login_path, session: { email: @user.email, password: 'password' } 
     assert_redirected_to @user 
     follow_redirect! 
     assert_template 'users/show' 
     assert_select "a[href=?]", login_path, count: 0 
     assert_select "a[href=?]", logout_path 
     assert_select "a[href=?]", user_path(@user) 
    end 

    test "login with valid information followed by logout" do 
    get login_path 
    post login_path, session: { email: @user.email, password: 'password' } 
    assert is_logged_in? 
    assert_redirected_to @user 
    follow_redirect! 
    assert_template 'users/show' 
    assert_select "a[href=?]", login_path, count: 0 
    assert_select "a[href=?]", logout_path 
    assert_select "a[href=?]", user_path(@user) 
    delete logout_path 
    assert_not is_logged_in? 
    assert_redirected_to root_url 
    follow_redirect! 
    assert_select "a[href=?]", login_path 
    assert_select "a[href=?]", logout_path,  count: 0 
    assert_select "a[href=?]", user_path(@user), count: 0 
    end 
end 

試過我最好解決相關的代碼。我認爲我已經在適當的地方正確定義了銷燬方法。這是我的會議幫手和控制器。

sessions_controller.rb:

第18行是 'redirect_to_root_url'

class SessionsController < ApplicationController 


def new 
    end 

    def create 
    user = User.find_by(email: params[:session][:email].downcase) 
    if user && user.authenticate(params[:session][:password]) 
     log_in user 
     redirect_to user 
    else 
     flash.now[:danger] = 'Invalid email/password combination' 
    render 'new' 
    end 
    end 

    def destroy 
    log_out 
    redirect_to_root_url 
    end 
end 

sessions_helper.rb:

module SessionsHelper 

    #Logs in the given user. 
    def log_in(user) 
     session[:user_id] = user.id 
    end 

    #Returns the current logged in user, if any 
    def current_user 
     @current_user ||= User.find_by(id: session[:user_id]) 
    end 

    #Returns true if the user is logged in, false otherwise 
    def logged_in? 
     !current_user.nil? 
    end 

    #Logs out current user 
    def log_out 
     session.delete(:user_id) 
     @current_user = nil 
    end 
end 
+0

哪個章節的部分,分段是你? –

+0

第8.3節退出,列表8.28正好卡住我的位置 –

+0

已編輯,錯誤現在有所不同,不知道爲什麼,但現在它可能更有意義。 –

回答

1

在你sessions_controller.rb文件更改:

def destroy 
    log_out 
    redirect_to_root_url 
end 

def destroy 
    log_out 
    redirect_to root_url 
end 
+1

啊,這麼簡單。謝謝 –

相關問題