0

我在做 Michael Hartl 的 Ruby on Rails 的第 8 章,我完成了这一章,最后运行了我的测试并得到了一些错误。我通过与 Michael Hartl 的 github 相关文件进行交叉检查以及查看他最近的截屏视频来检查我的代码是否正确,但我似乎无法找出错误。

能否请您告诉我错误和解决方案,例如我需要更改的内容?

错误

2) 使用有效信息进行身份验证登录

    ←[31mFailure/Error:←[0m ←[31mbefore { sign_in user }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `redirect_back_or' for #<SessionsController:0x4366b
d8>←[0m
←[36m     # ./app/controllers/sessions_controller.rb:10:in `create'←[0m
←[36m     # (eval):2:in `click_button'←[0m
←[36m     # ./spec/support/utilities.rb:13:in `sign_in'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels)
 in <top (required)>'←[0m

3) 使用有效信息进行身份验证登录

     ←[31mFailure/Error:←[0m ←[31mbefore { sign_in user }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `redirect_back_or' for #<SessionsController:0x43fdd
60>←[0m
←[36m     # ./app/controllers/sessions_controller.rb:10:in `create'←[0m
←[36m     # (eval):2:in `click_button'←[0m
←[36m     # ./spec/support/utilities.rb:13:in `sign_in'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels)
 in <top (required)>'←[0m

4) 使用有效信息进行身份验证登录

    ←[31mFailure/Error:←[0m ←[31mbefore { sign_in user }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `redirect_back_or' for #<SessionsController:0x44773
38>←[0m
←[36m     # ./app/controllers/sessions_controller.rb:10:in `create'←[0m
←[36m     # (eval):2:in `click_button'←[0m
←[36m     # ./spec/support/utilities.rb:13:in `sign_in'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels)
 in <top (required)>'←[0m

5) 使用有效信息进行身份验证登录

    ←[31mFailure/Error:←[0m ←[31mbefore { sign_in user }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `redirect_back_or' for #<SessionsController:0x4153f
f8>←[0m
←[36m     # ./app/controllers/sessions_controller.rb:10:in `create'←[0m
←[36m     # (eval):2:in `click_button'←[0m
←[36m     # ./spec/support/utilities.rb:13:in `sign_in'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels)
 in <top (required)>'←[0m

6) 使用有效信息进行身份验证登录

    ←[31mFailure/Error:←[0m ←[31mbefore { sign_in user }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `redirect_back_or' for #<SessionsController:0x4347f
a8>←[0m
←[36m     # ./app/controllers/sessions_controller.rb:10:in `create'←[0m
←[36m     # (eval):2:in `click_button'←[0m
←[36m     # ./spec/support/utilities.rb:13:in `sign_in'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels)
 in <top (required)>'←[0m

7) 使用有效信息进行身份验证登录

    ←[31mFailure/Error:←[0m ←[31mbefore { sign_in user }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `redirect_back_or' for #<SessionsController:0x43e1e
d8>←[0m
←[36m     # ./app/controllers/sessions_controller.rb:10:in `create'←[0m
←[36m     # (eval):2:in `click_button'←[0m
←[36m     # ./spec/support/utilities.rb:13:in `sign_in'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels)
 in <top (required)>'←[0m

8) 使用有效信息进行身份验证登录,然后退出

    ←[31mFailure/Error:←[0m ←[31mbefore { sign_in user }←[0m
     ←[31mNoMethodError:←[0m
       ←[31mundefined method `redirect_back_or' for #<SessionsController:0x446dc
18>←[0m
←[36m     # ./app/controllers/sessions_controller.rb:10:in `create'←[0m
←[36m     # (eval):2:in `click_button'←[0m
←[36m     # ./spec/support/utilities.rb:13:in `sign_in'←[0m
←[36m     # ./spec/requests/authentication_pages_spec.rb:31:in `block (4 levels)
 in <top (required)>'←[0m

session_conrtoller.rb

class SessionsController < ApplicationController

  def new
  end

  def create
    user = User.find_by_email(params[:session][:email])
    if user && user.authenticate(params[:session][:password])
       sign_in user
       redirect_back_or user
    else
       flash.now[:error] = "Invalid email/password combination"
       render 'new'
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end

实用程序.rb

include ApplicationHelper

RSpec::Matchers.define :have_error_message do |message|
   match do |page|
    page.should have_selector('div.alert.alert-error', text: 'Invalid')
  end
end

def sign_in(user)
  visit signin_path
  fill_in "Email",    with: user.email
  fill_in "Password", with: user.password
  click_button "Sign in"

  # Sign in when not using Capybara.
  cookies[:remember_token] = user.remember_token
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_error_message }

      describe "after visiting another page" do
        before { click_link "Home" }
        it { should_not have_error_message }
      end
    end

    describe "with valid information" do
      let(:user) { FactoryGirl.create(:user) }
      before { sign_in user }

      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 have_link('Settings',    href: edit_user_path(user)) }
      it { should have_link('Users',       href: users_path) }
      it { should_not have_link('Sign in', href: signin_path) }

      describe "followed by signout" do
        before { click_link "Sign out" }
        it { should have_link('Sign in') }
      end
    end
  end
end 
4

3 回答 3

0

看起来你有未定义的方法我会在redirect_back_or. 调试继续....

于 2012-06-07T15:10:23.467 回答
0

我相信该redirect_back_or方法是在 app/helpers/sessions_helper.rb 中定义的。

由于您尚未在上面列出该代码,因此我将开始查找那里。

于 2012-06-07T15:42:09.100 回答
0

我跟着这个项目的早期版本,就像 edr 说的那样,该方法redirect_back_or是在 app/helpers/sessions_helper.rb 中定义的。请检查它是否在那里,然后您的应用程序应该可以正常运行。

于 2012-06-07T17:08:50.290 回答