Reputation: 333
I am doing Michael Hartls Ruby on Rails Tutorial and I am getting a routing error when I try to browse to localhost:3000/sessions
Routing Error
No route matches "/sessions"
From the tutorial, I was under the impression that rails will infer the route to "sessions" and I would not need to add a specification route to routes.rb.
If I run rake routes I get the following
users GET /users(.:format) {:action=>"index", :controller=>"users"}
POST /users(.:format) {:action=>"create", :controller=>"users"}
new_user GET /users/new(.:format) {:action=>"new", :controller=>"users"}
edit_user GET /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user GET /users/:id(.:format) {:action=>"show", :controller=>"users"}
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
DELETE /users/:id(.:format) {:action=>"destroy", :controller=>"users"}
sessions POST /sessions(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /sessions/new(.:format) {:action=>"new", :controller=>"sessions"}
session DELETE /sessions/:id(.:format) {:action=>"destroy", :controller=>"sessions"}
root /(.:format) {:controller=>"pages", :action=>"home"}
signup /signup(.:format) {:controller=>"users", :action=>"new"}
signin /signin(.:format) {:controller=>"sessions", :action=>"new"}
signout /signout(.:format) {:controller=>"sessions", :action=>"destroy"}
about /about(.:format) {:controller=>"pages", :action=>"about"}
contact /contact(.:format) {:controller=>"pages", :action=>"contact"}
help /help(.:format) {:controller=>"pages", :action=>"help"}
My routes.rb contains
SampleApp::Application.routes.draw do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
root :to => 'pages#home'
match '/signup', :to => 'users#new'
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/about', :to => 'pages#about'
match '/contact', :to => 'pages#contact'
match '/help', :to => 'pages#help'
I can get it to work if I add the following line to routes.rb, but I didn't think I needed to do this explicitly
match '/sessions',:to => 'sessions#create'
Am I missing something or misunderstanding something?
I am running Rails 3.0.11 and Ruby 1.9.2p290
Upvotes: 1
Views: 857
Reputation: 16960
In rails a GET
verb request to /sessions
routes to the index
action.
You either need to browse to /sessions/new
, or add the additional match like you've done in the last part of your question.
These are the default routes for a resource
: http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions
Upvotes: 4