rails NoMethodError: undefined method " " for nil:NilClass
rails NoMethodError: undefined method " " for nil:NilClass
我正在编写 Michael Hartl (chapter 9) 的 rails 教程。
我收到这个错误:
Error:
UsersLoginTest#test_login_with_remembering:
NoMethodError: undefined method `FILL_IN' for nil:NilClass
test/integration/users_login_test.rb:42:in `block in <class:UsersLoginTest>'
在chapter 8中添加了FILL-IN,直到第9章才出现任何问题。
我尝试了很多,但我无法修复它。这是我的 UsersLoginTest class.
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 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 clicking logout 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 assigns(:user).FILL_IN, FILL_IN
end
test "login without remembering" do
log_in_as(@user, remember_me: '0')
assert_nil cookies['remember_token']
end
end
第 42 行是:
assert_equal assigns(:user).FILL_IN,FILL_IN
FILL_IN
在这种情况下是一个占位符。 Michael 希望您用正确的文本替换它。
错误显示 assigns(:user)
正在返回 nil。
assigns
测试控制器中设置的实例变量。这里的期望是您在控制器中为 @user
设置了一个值,并且当对其调用方法 FILL_IN
时它将等于 FILL_IN
。解决 @user
成为 nil
的问题后,您需要实际填写这些占位符。
如果您在清单 8.62 中执行了 "login with remembering exercise",则需要在清单 9.29 中使用 @user 实例变量。我花了一段时间才意识到这一点,所以它可能会帮助其他初学者在代码中看到 Shadwell 的建议:
sessions_controller.rb
class SessionsController < ApplicationController
# ...
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' # Flash.now designed for rendered pages, disappears at the next request.
render 'new'
end
end
# ...
end
users_login_test.rb
# ...
test "login with remembering" do
log_in_as(@user, remember_me: '1')
assert_equal assigns(:user).remember_token, cookies['remember_token']
end
# ...
公平地说,Michael Hartl 建议我们用 git 解决单独主题分支中的练习。
我正在编写 Michael Hartl (chapter 9) 的 rails 教程。
我收到这个错误:
Error:
UsersLoginTest#test_login_with_remembering:
NoMethodError: undefined method `FILL_IN' for nil:NilClass
test/integration/users_login_test.rb:42:in `block in <class:UsersLoginTest>'
在chapter 8中添加了FILL-IN,直到第9章才出现任何问题。 我尝试了很多,但我无法修复它。这是我的 UsersLoginTest class.
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 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 clicking logout 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 assigns(:user).FILL_IN, FILL_IN
end
test "login without remembering" do
log_in_as(@user, remember_me: '0')
assert_nil cookies['remember_token']
end
end
第 42 行是:
assert_equal assigns(:user).FILL_IN,FILL_IN
FILL_IN
在这种情况下是一个占位符。 Michael 希望您用正确的文本替换它。
错误显示 assigns(:user)
正在返回 nil。
assigns
测试控制器中设置的实例变量。这里的期望是您在控制器中为 @user
设置了一个值,并且当对其调用方法 FILL_IN
时它将等于 FILL_IN
。解决 @user
成为 nil
的问题后,您需要实际填写这些占位符。
如果您在清单 8.62 中执行了 "login with remembering exercise",则需要在清单 9.29 中使用 @user 实例变量。我花了一段时间才意识到这一点,所以它可能会帮助其他初学者在代码中看到 Shadwell 的建议:
sessions_controller.rb
class SessionsController < ApplicationController
# ...
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' # Flash.now designed for rendered pages, disappears at the next request.
render 'new'
end
end
# ...
end
users_login_test.rb
# ...
test "login with remembering" do
log_in_as(@user, remember_me: '1')
assert_equal assigns(:user).remember_token, cookies['remember_token']
end
# ...
公平地说,Michael Hartl 建议我们用 git 解决单独主题分支中的练习。