第8章Mark Hartl教程未定义'remember_token'错误

时间:2013-06-22 16:07:19

标签: ruby-on-rails testing rspec railstutorial.org

我一直在努力让我的考试通过。我无法找到问题,任何帮助都将不胜感激!

这是失败消息。

    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

这是会话助手

    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

这是会话控制器

    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

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

1 个答案:

答案 0 :(得分:1)

[来自原始答案的每条评论帖子更新]。

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

一般来说,我发现教程是&#34;发现&#34;。如果你仔细阅读文字,你就不会出错。