获取此错误,主要信息是:未定义的本地变量或方法'用户'对于SessionsController ,在'创建'下单挑第10行在每种情况下在所述控制器中。
现在我已经找到了自己的问题解决方法,即改变行' redirect_back_or用户'到' redirect_back_or @ user'在会话控制器中(第10行)。然而,这违反了Hartl的指示,因为' redirect_back_or用户'在说明中是明确的。
想知道我是否在其他地方发生了错误,如果修复了,则会让“redirect_back_or user'工作正常。
ERROR["test_successful_edit_with_friendly_forwarding", UsersEditTest, 2016-02-10 03:42:25 -0500]
test_successful_edit_with_friendly_forwarding#UsersEditTest (1455093745.41s)
NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x007fc46b54ed40>
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_edit_test.rb:21:in `block in <class:UsersEditTest>'
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_edit_test.rb:21:in `block in <class:UsersEditTest>'
ERROR["test_unsuccessful_edit", UsersEditTest, 2016-02-10 03:42:25 -0500]
test_unsuccessful_edit#UsersEditTest (1455093745.44s)
NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x007fc4701e7530>
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_edit_test.rb:9:in `block in <class:UsersEditTest>'
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_edit_test.rb:9:in `block in <class:UsersEditTest>'
ERROR["test_login_with_remembering", UsersLoginTest, 2016-02-10 03:42:25 -0500]
test_login_with_remembering#UsersLoginTest (1455093745.64s)
NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x007fc471c04d78>
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_login_test.rb:50:in `block in <class:UsersLoginTest>'
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_login_test.rb:50:in `block in <class:UsersLoginTest>'
ERROR["test_login_without_remembering", UsersLoginTest, 2016-02-10 03:42:25 -0500]
test_login_without_remembering#UsersLoginTest (1455093745.68s)
NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x007fc46b9ce370>
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_login_test.rb:55:in `block in <class:UsersLoginTest>'
app/controllers/sessions_controller.rb:10:in `create'
test/test_helper.rb:23:in `log_in_as'
test/integration/users_login_test.rb:55:in `block in <class:UsersLoginTest>'
ERROR["test_login_with_valid_information_followed_by_logout", UsersLoginTest, 2016-02-10 03:42:25 -0500]
test_login_with_valid_information_followed_by_logout#UsersLoginTest (1455093745.71s)
NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x007fc47096d4f8>
app/controllers/sessions_controller.rb:10:in `create'
test/integration/users_login_test.rb:31:in `block in <class:UsersLoginTest>'
app/controllers/sessions_controller.rb:10:in `create'
test/integration/users_login_test.rb:31:in `block in <class:UsersLoginTest>'
ERROR["test_login_with_valid_information", UsersLoginTest, 2016-02-10 03:42:25 -0500]
test_login_with_valid_information#UsersLoginTest (1455093745.74s)
NameError: NameError: undefined local variable or method `user' for #<SessionsController:0x007fc471ba7f10>
app/controllers/sessions_controller.rb:10:in `create'
test/integration/users_login_test.rb:20:in `block in <class:UsersLoginTest>'
app/controllers/sessions_controller.rb:10:in `create'
test/integration/users_login_test.rb:20:in `block in <class:UsersLoginTest>'
会话控制器
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
params[:session][:remember_me] == '1' ? remember(@user) : forget(@user)
redirect_back_or user
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
Sessions Helper
# Returns the user corresponding to the remember token cookie
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(cookies[:remember_token])
log_in user
@current_user = user
end
end
end
# Returns true if the user is logged in, false otherwise
def logged_in?
!current_user.nil?
end
# Forgets a persistent session
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
# Logs out current user
def log_out
forget(current_user)
session.delete(:user_id)
@current_user = nil
end
# Redirects to stored location (or to the default).
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.url if request.get?
end
end
用户登录测试
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
# Simulate a user logging out in a second window
delete logout_path
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
test "login with remembering" do
log_in_as(@user, remember_me: '1')
assert_equal cookies['remember_token'], assigns(:user).remember_token
end
test "login without remembering" do
log_in_as(@user, remember_me: '0')
assert_nil cookies['remember_token']
end
end
用户编辑测试
require 'test_helper'
class UsersEditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "unsuccessful edit" do
log_in_as(@user)
get edit_user_path(@user)
assert_template 'users/edit'
patch user_path(@user), user: { name: "",
email: "foo@invalid",
password: "foo",
password_confirmation: "bar" }
assert_template 'users/edit'
end
test "successful edit with friendly forwarding" do
get edit_user_path(@user)
log_in_as(@user)
assert_redirected_to edit_user_path(@user)
name = "Foo Bar"
email = "foo@bar.com"
patch user_path(@user), user: { name: name,
email: email,
password: "",
password_confirmation: "" }
assert_not flash.empty?
assert_redirected_to @user
@user.reload
assert_equal name, @user.name
assert_equal email, @user.email
end
end
答案 0 :(得分:1)
在会话控制器中,尝试将@user
操作下的create
实例变量更改为user
。
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
if user.activated?
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_back_or user
else
然后运行测试并查看错误是否仍然存在。