簡體   English   中英

第 8 章 Rails 教程記住令牌錯誤

[英]Chapter 8 Rails Tutorial Remember Token Errors

本章介紹添加記住令牌以確保記住用戶登錄狀態,並且僅當用戶明確注銷時才清除 session。 我了解在我的應用程序中擁有此功能的重要性,因此想確保它正常工作。 但是,當我運行時,我遇到了很多錯誤

$ bundle exec rspec spec/

我懷疑它們與我的用戶 model 有關,因為除了一個以外,它們都包含:

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

您是否生成了將列添加到用戶模型的遷移?

$ rails generate migration add_remember_token_to_users

在此之后編輯遷移文件以添加新字段remember_token

之后你需要做

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

您在模型上的注釋不顯示該列。 確保執行了上述命令。

我得到了類似的測試失敗錯誤。 我所做的是生成一個帶字符串類型的remember_token列。

rails generate migration add_remember_token_to_users remember_token:string --force

rake db:migrate RAILS_ENV=test

之后,測試通過。

我在學習Mhartl教程時遇到了同樣的問題。我解決了這個問題。 當您的數據庫已經有一些用戶數據時,這個問題就顯示出來了,那么“remember_token”migrate將無法正常工作。 你必須清理你的日期,然后做db:migrate.like:

rake db:drop db:create rake db:migrate(小心:這會擦除你的所有數據)

希望有所幫助

不知道為什么沒有提到它(或者我錯過了它)。 “記住登錄”測試有一行“assert_equal cookies['remember_token'], assigns(:user).remember_token”。 要使測試通過,您需要做的就是用“assert_not cookies[:remember_token].blank?”替換該行。

我遇到了同樣的問題,得到了未定義的方法find_by_remember_token。

這是我做的修復它:

heroku run rake db:migrate

然后再次推送到heroku

git push heroku

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM