私は、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