3

I am currently working through the Rails Tutorial by Michael Hartl and have run into an error I can't understand.

There is a section where the tutorial explains how to create custom URI's in the routes.rb file. The tutorial explains that by coding this:

match '/about', to: 'static_pages#about'

named routes should automatically be created that look like this:

about_path => '/about'
about_url  => 'http://localhost:3000/about'

The following section then helps you work through fixing up rspec tests by replacing certain bits of code with these variables. But when I do this, I end up failing every test because all the "automatically created variables" can't be found...

Can anyone explain to me why they aren't being found or where I can look to see if they were in fact created already.

Paul Fioravanti
  • 16,423
  • 7
  • 71
  • 122
Shawn Taylor
  • 1,434
  • 5
  • 26
  • 36
  • 1
    `rake routes` is your friend if you want to see all known routes, if you're using 3.2+ you can also have a look at the sextant gem https://github.com/schneems/sextant – pjam Jan 07 '13 at 01:30
  • You might also need to add the `as: 'name'` option : http://guides.rubyonrails.org/routing.html#naming-routes – pjam Jan 07 '13 at 01:31
  • You should also be using the word "get" instead of match, as this defines what type of request is expected. – James Jan 07 '13 at 01:32
  • 2
    When you added the route, did you remember to restart your Rails server and restart Spork? – Paul Fioravanti Jan 07 '13 at 06:17

4 Answers4

11

As far as I'm aware, if you do not use as:, you do not get a named route. See the guides for more info.

In your example, you could do the following:

get '/about', to: 'static_pages#about', as: 'about'
theIV
  • 25,434
  • 5
  • 54
  • 58
  • when i do this, and use rake routes to check if about_path has been created, what do i look for in the resulting dialog? – Shawn Taylor Jan 07 '13 at 04:04
  • @ShawnTaylor all the way to the left, you should see the word `about`. Kind of like `about GET /about(.:format) static_pages#about`. – theIV Jan 07 '13 at 04:47
1

Are you using spork? Try restarting the spork server

RSpec not finding my named routes

Community
  • 1
  • 1
John Ong
  • 31
  • 2
0

The instruction is to add a line like this:

match '/about', to: 'static_pages#about', via: 'get'

Note the , via: 'get'.

givanse
  • 14,503
  • 8
  • 51
  • 75
0

I arriving late to this thread, but I had the same problem in the same point of the tutorial, but as @pjam mentioned before, I think we needed to add the as: 'about' option in the routes.rb file, along with the others for help and contact, as mentioned in the tutorial. It solved the problem with the tests for me, now they all pass.