【问题标题】:chapter 8 Mark Hartl tutorial undefined 'remember_token' error第 8 章 Mark Hartl 教程 undefined 'remember_token' 错误
【发布时间】:2013-06-22 16:07:18
【问题描述】:

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

这是失败消息。

    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)>'

这里是认证页面规范:

    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

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

【问题讨论】:

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


    【解决方案1】:

    [根据原始答案的每个评论线程更新]。

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

    总的来说,我发现该教程是“准确的”。仔细看文字就不会出错。

    【讨论】:

    • 这就是我认为的错误,但已经做到了,我已经编辑了我的问题以显示用户模型
    • 错误消息显示Class 对象的实例,而不是User 对象,正如我所料。您是否检查了 factories.rb 配置中的 :user
    • 我输入的数据库中的第一个用户是一个自定义用户,有我自己的电子邮件和密码,但是书中 factory.rb 的 :user 是 mark hartl,将这些详细信息更改为自定义用户我创建但没有区别。
    • 您是否介意分享您的 session_controller.rb 文件,以便我们查看 create 内部发生的情况,以便将 Class 对象传递给 sign_in
    • 啊,你用User(类)而不是user(类的实例)调用sign。
    猜你喜欢
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多