我在做 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