【问题标题】:Rails Tutorial (M. Hartl) Chapter 8 Error: Undefined local variableRails 教程(M. Hartl)第 8 章错误:未定义的局部变量
【发布时间】:2016-02-10 15:03:29
【问题描述】:

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

在 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”

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

【问题讨论】:

  • 你在本章的哪个部分和小节?
  • 第 8.3 节注销,清单 8.28 正是我卡住的地方
  • 已编辑,现在错误有所不同,不知道为什么,但现在可能更有意义。

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


【解决方案1】:

在您的sessions_controller.rb 文件中更改:

def destroy
  log_out
  redirect_to_root_url
end

def destroy
  log_out
  redirect_to root_url
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多