1

我一直无法通过考试。我找不到问题是什么任何帮助将不胜感激!

这是失败消息。

    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

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

4

1 回答 1

1

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

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

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

于 2013-06-22T16:15:36.157 回答