gpt4 book ai didi

ruby-on-rails - 第 8 章 Mark Hartl 教程未定义 'remember_token' 错误

转载 作者:行者123 更新时间:2023-11-28 20:44:41 26 4
gpt4 key购买 nike

我一直无法通过测试。我找不到问题所在,我们将不胜感激!

这是失败信息。

    1) Authentication signin with valid information 
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

2) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

3) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

4) Authentication signin with valid information
Failure/Error: click_button "Sign in"
NoMethodError:
undefined method `remember_token' for #<Class:0x007fe30d0a93b0>
# ./app/helpers/sessions_helper.rb:4:in `sign_in'
# ./app/controllers/sessions_controller.rb:9:in `create'
# (eval):2:in `click_button'
# ./spec/requests/authentication_pages_spec.rb:34:in `block (4 levels) in <top (required)>'

这是身份验证页面规范 rb:

    require 'spec_helper'

describe "Authentication" do

subject { page }

describe "signin page" do
before { visit signin_path }

it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('title', text: 'Sign in') }
end

describe "signin" do
before { visit signin_path }

describe "with invalid information" do
before { click_button "Sign in" }

it { should have_selector('title', text: 'Sign in') }
it { should have_selector('div.alert.alert-error', text: 'Invalid') }
end

describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.alert.alert-error') }
end

describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email.upcase
fill_in "Password", with: user.password
click_button "Sign in"
end

it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
end

end


end

这是 session 助手

    module SessionsHelper

def sign_in(user)
cookies.permanent[:remember_token] = user.remember_token
self.current_user = user
end

def signed_in?
!current_user.nil?
end

def current_user=(user)
@current_user = user
end

def current_user
@current_user ||= User.find_by_remember_token(cookies[:remember_token])
end
end

这里是用户rb文件

    class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password

before_save { |user| user.email = email.downcase }
before_save :create_remember_token

validates :name, presence: true, length: {maximum: 50}
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: {minimum: 6}
validates :password_confirmation, presence:true


private

def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end

这是 session Controller

    class SessionsController < ApplicationController

def new
end

def create
user = User.find_by_email(params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in User
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end

def destroy
end
end

这些是我认为有问题的文件,如果您需要查看任何其他文件,请告诉我。任何想法将不胜感激!谢谢!

最佳答案

[根据原始答案更新每个评论线程]。

错误消息显示 Class 缺少该方法,这意味着 Class 而不是 User 的实例作为sign_in 的参数。查看sessions_controller.rb中的调用代码,可以看到传递的是User,而不是user

总的来说,我发现该教程“非常准确”。如果您仔细阅读文本,就不会出错。

关于ruby-on-rails - 第 8 章 Mark Hartl 教程未定义 'remember_token' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17252681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com