gpt4 book ai didi

ruby-on-rails - 第 8 章 Rails 教程记住 token 错误

转载 作者:行者123 更新时间:2023-12-04 06:33:03 25 4
gpt4 key购买 nike

本章是关于添加记住 token 以确保记住用户登录状态并且只有在用户明确退出时才会清除 session 。我了解在我的应用程序中使用此功能的重要性,因此想确保它正常工作。但是,当我运行时,我遇到了一堆错误

$ bundle exec rspec spec/

我怀疑它们与我的用户模型有关,因为它们都包含:
NoMethodError:
undefined method `remember_token=' for #<User:...>

最后一个包含
Failure/Error: it { should respond_to(:remember_token) }

然后指向我的 user_spec.rb、user.rb 和 authentication_pages_spec.rb 文件,我在此处包含了大部分(相关部分)。

用户.rb:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#

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

authentication_pages_spec.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')}

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

describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before do
fill_in "Email", with: user.email
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

和 user_spec.rb 的开头:
# == Schema Information
#
# Table name: users
#
# id :integer not null, primary key
# name :string(255)
# email :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#

require 'spec_helper'

describe User do

before do
@user = User.new(name: "Example User", email: "user@example.com",
password: "foobar", password_confirmation: "foobar")
end

subject { @user }

it { should respond_to(:name) }
it { should respond_to(:email) }
it { should be_valid }
it { should respond_to(:password_digest) }
it { should respond_to(:password) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:authenticate) }
it { should respond_to(:remember_token) }

describe "remember token" do
before { @user.save }
its(:remember_token) { should_not be_blank }
end
.
.
.

任何帮助将非常感激!

最佳答案

如果您在 Heroku 的生产环境中遇到此错误,请在运行后:

heroku run rake db:migrate

您需要重新启动您的应用程序:
heroku restart

关于ruby-on-rails - 第 8 章 Rails 教程记住 token 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10374416/

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