I was doing chapter 8 of Ruby on Rails by Michael Hartl, I finished the chapter, finally ran my test and got a few errors. I checked if my codes were right by cross checking with Michael Hartl’s github relevant files and also by viewing his recent screencast and yet I cannot seem to figure out the error.
Could you please kindly tell me the error and the solution like what I need to change?
ERRORS
2) Authentication signin with valid information
←[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) Authentication signin with valid information
←[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) Authentication signin with valid information
←[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) Authentication signin with valid information
←[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) Authentication signin with valid information
←[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) Authentication signin with valid information
←[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) Authentication signin with valid information followed by signout
←[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
utilities.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
I followed along to an earlier version of this project and like edr says, that method
redirect_back_orwas defined in app/helpers/sessions_helper.rb.Please check to see that its there, then your app should run fine.