第8章Rails教程记住令牌错误

时间:2012-04-29 17:45:57

标签: ruby-on-rails remember-me

本章介绍如何添加记忆令牌以确保记住用户登录状态,并且只有在用户明确注销时才会清除会话。我理解在我的应用中使用此功能的重要性,因此要确保它正常工作。当我运行

时,我遇到了一堆错误
$ 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文件,我在这里包含了大部分(相关部分)。

user.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
.
.
.

非常感谢任何帮助!

5 个答案:

答案 0 :(得分:4)

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

heroku run rake db:migrate

您需要重新启动应用:

heroku restart

答案 1 :(得分:2)

您是否生成了将列添加到用户模型的迁移?

$ rails generate migration add_remember_token_to_users

此后编辑迁移文件以添加新字段remember_token

之后你需要做

$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare

模型上的注释不显示列。确保执行了上述命令。

答案 2 :(得分:2)

我得到了类似的测试失败错误。 我所做的是用字符串类型生成remember_token列。

rails generate migration add_remember_token_to_users remember_token:string --force

rake db:migrate RAILS_ENV=test

之后,测试通过。

答案 3 :(得分:0)

我在学习Mhartl教程时遇到了同样的问题。我解决了这个问题。当你的数据库已经拥有一些用户数据时,这个问题出现了,然后&#34; remember_token&#34;迁移不起作用。你必须清理你的日期,然后做db:migrate.like this:

rake db:drop db:create rake db:migrate(小心:这会擦除你的所有数据)

希望帮助

答案 4 :(得分:-1)

我遇到了同样的问题,得到了未定义的方法find_by_remember_token。

以下是我要解决的问题:

heroku run rake db:migrate

然后再次推送到heroku

git push heroku