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.

4
  • 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 github.com/schneems/sextant
    – pjam
    Jan 7, 2013 at 1:30
  • You might also need to add the as: 'name' option : guides.rubyonrails.org/routing.html#naming-routes
    – pjam
    Jan 7, 2013 at 1:31
  • You should also be using the word "get" instead of match, as this defines what type of request is expected.
    – James
    Jan 7, 2013 at 1:32
  • 2
    When you added the route, did you remember to restart your Rails server and restart Spork? Jan 7, 2013 at 6:17

4 Answers 4

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'
2
  • 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? Jan 7, 2013 at 4: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 7, 2013 at 4:47
1

Are you using spork? Try restarting the spork server

RSpec not finding my named routes

0

The instruction is to add a line like this:

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

Note the , via: 'get'.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.