Rails Tutorial (M. Hartl) Chapter 8 Error: Undefined local variable
Rails Tutorial (M. Hartl) Chapter 8 Error: Undefined local variable
已编辑:现在出现不同的错误,发布的代码仍然相同。
关于 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 行和我的会话控制器中的第 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 行将是 'delete 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
已尽力修复相关代码。我认为我在正确的位置正确定义了 destroy 方法。这是我的会话助手和控制器。
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
在您的 sessions_controller.rb
文件中更改:
def destroy
log_out
redirect_to_root_url
end
至
def destroy
log_out
redirect_to root_url
end
已编辑:现在出现不同的错误,发布的代码仍然相同。
关于 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 行和我的会话控制器中的第 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 行将是 'delete 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
已尽力修复相关代码。我认为我在正确的位置正确定义了 destroy 方法。这是我的会话助手和控制器。
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
在您的 sessions_controller.rb
文件中更改:
def destroy
log_out
redirect_to_root_url
end
至
def destroy
log_out
redirect_to root_url
end