已编辑:现在发生的不同错误,发布的代码仍然相同。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
哪个章节的部分,分段是你? –
第8.3节退出,列表8.28正好卡住我的位置 –
已编辑,错误现在有所不同,不知道为什么,但现在它可能更有意义。 –