2013-06-22 101 views
1

我一直有麻煩試圖讓我的測試通過。我無法找到問題是什麼幫助將不勝感激!第8章馬克哈特爾教程undefined'remember_token'錯誤

這是失敗消息。

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+\-.][email protected][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

[原文更新爲每條評論主題]。

該錯誤消息表示Class爲丟失的方法中,這意味着Class而非User實例被作爲參數傳遞給sign_in。檢查sessions_controller.rb中的呼叫代碼,可以看到User已通過,而不是user

一般來說,我發現教程是「專注」。如果你仔細地閱讀文本,你就不會出錯。

+0

這就是我認爲過錯的地方,但已經完成了它,我編輯了我的問題以顯示用戶模型 – cyclopse87

+0

錯誤消息顯示了一個'Class'對象,而不是'User'對象的一個​​實例,因爲我會期望。你檢查':user'的'factories.rb'配置嗎? –

+0

我輸入的數據庫中的第一個用戶是具有我自己的電子郵件和密碼的自定義用戶,但factory.rb的book中的:user是mark hartl,將這些詳細信息更改爲我創建的自定義用戶,但沒有任何區別。 – cyclopse87

相關問題