1

I have started working on Chapter 8 of the famous Rails tutorial. I think I followed the instructions closely and defined the following routes:

SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root  'static_pages#home'
match '/signup',  to: 'users#new',            via: 'get'
match '/signin',  to: 'sessions#new',         via: 'get'
match '/signout', to: 'sessions#destroy',     via: 'delete'
match '/help',    to: 'static_pages#help',    via: 'get'
match '/about',   to: 'static_pages#about',   via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'

The session controller (/controllers/sessions_controller.rb) is defined as follows:

class SessionsController < ApplicationController
def new
end
def create
end
def destroy
end
end

In spec/requests/authentication_pages_spec.rb I have created the following test:

require 'spec_helper'
describe "Authentication" do
subject {page}
  describe "signin page" do
    before { visit signin_path}
    it { should have_content('Sign in')}
    it { should have_title('Sign in')}
  end
end

The test causes the following errors:

Failures:
1) Authentication signin page 
 Failure/Error: before { visit signin_path}
 NameError:
   undefined local variable or method `signin_path' for # 
   <RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec05fe8>
   # ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'
2) Authentication signin page 
 Failure/Error: before { visit signin_path}
 NameError:
   undefined local variable or method `signin_path' for # 
   <RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f98dec5cf50>
   # ./spec/requests/authentication_pages_spec.rb:7:in `block (3 levels) in <top (required)>'

It seems the signin_path named route is not recognised even though it is defined in routes.rb. I replaced that named route with one of the others (signup_path) and the problem disappeared. So, it is something about this particular named route. Can you tell what the problem is?

rake routes produces the following output:

sb7904313:sample_app nnikolo$ rake routes
 Prefix Verb   URI Pattern               Controller#Action
     users GET    /users(.:format)          users#index
           POST   /users(.:format)          users#create
  new_user GET    /users/new(.:format)      users#new
 edit_user GET    /users/:id/edit(.:format) users#edit
      user GET    /users/:id(.:format)      users#show
           PATCH  /users/:id(.:format)      users#update
           PUT    /users/:id(.:format)      users#update
           DELETE /users/:id(.:format)      users#destroy
 sessions    POST   /sessions(.:format)       sessions#create
 new_session GET    /sessions/new(.:format)   sessions#new
 session DELETE /sessions/:id(.:format)   sessions#destroy
    root GET    /                         static_pages#home
  signup GET    /signup(.:format)         users#new
  signin GET    /signin(.:format)         sessions#new
 signout DELETE /signout(.:format)        sessions#destroy
    help GET    /help(.:format)           static_pages#help
   about GET    /about(.:format)          static_pages#about
 contact GET    /contact(.:format)        static_pages#contact

I also re-started the server and it did not solve the problem (I made a post to the contrary but I was wrong).

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Nick
  • 2,924
  • 4
  • 36
  • 43

4 Answers4

4

Try this instead. You can access your route names by the command rake routes. Since it's coming from your sessions controller, by default the route is probably something like new_session_path. To change it, you need to specify what you what to change it to in your routes with as: new_name

match '/signin',  to: 'sessions#new',         via: 'get', as: 'signin'
Justin
  • 4,922
  • 2
  • 27
  • 69
  • Can you do `rake routes` and let me know what Prefix is for that particular path? – Justin May 07 '14 at 14:27
  • Also, I haven't used the _as:_ form for any of the routes - yet replacing _signin_path_ with _signup_path_ works – Nick May 07 '14 at 14:30
  • I added the rake routes output to the post. – Nick May 07 '14 at 14:33
  • The other routes seem to be OK. As said, replacing signin_path with signup_path solves the problem (the test fails for a different reason). – Nick May 07 '14 at 14:39
  • Unfortunately, I was wrong about re-starting solving the problem - I also have substituted the **signin_path** with the **signup_path** - this is what was making the difference, not the re-start. I am back at square one with this one. – Nick May 07 '14 at 15:19
0

Try:--

get "signin" => "sessions#new", :as => "signin"
resources :sessions

Use the route as new_session_path or signin_path

Shamsul Haque
  • 2,441
  • 2
  • 18
  • 20
0

See this thread: undefined method `visit' when using RSpec and Capybara in rails

You possibly did not have the line config.include Capybara:DSL in your spec_helper.rb and something about putting the tests inside rspec/features/ due to the latest changes in Capybara.

Community
  • 1
  • 1
Daniel Sutantyo
  • 183
  • 1
  • 1
  • 9
-1

I was wrong about the re-start solving the problem so retract this post.

==EDIT I found the culprit for this erratic behaviour of rspec: rails does not seem to empty the cache between tests (which is, in my opinion, a scary bug). By default it fails to re-read the files and thus may ignore changes that have occurred. I put more details here: Rails tutorial: Rails tutorial: undefined method

Community
  • 1
  • 1
Nick
  • 2,924
  • 4
  • 36
  • 43