Rails教程(M. Hartl)第8章错误:未定义的局部变量

时间:2016-02-10 05:26:55

标签: ruby-on-rails ruby railstutorial.org

编辑:现在出现不同的错误,代码发布仍然相同。

Hartl的Ruby教程第8.3章。在rake测试中获取此错误以进行有效的登录测试。

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

错误消息指向我的登录测试中的第39行和我的Sessions控制器中的第18行作为错误,如您所见:

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&#39;

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 

尽我所能修复相关代码。我想我已经在正确的位置正确定义了destroy方法。这是我的会话助手和控制器。

sessions_controller.rb:

第18行将是&#39; redirect_to_root_url&#39;

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

1 个答案:

答案 0 :(得分:1)

sessions_controller.rb文件更改中:

def destroy
  log_out
  redirect_to_root_url
end

def destroy
  log_out
  redirect_to root_url
end