Ruby on Rails教程 - '/ sessions'上的路由错误

时间:2012-02-28 13:33:42

标签: ruby-on-rails

我正在做Michael Hartls Ruby on Rails Tutorial,当我尝试浏览localhost时遇到路由错误:3000 / sessions

路由错误

没有路线匹配“/ sessions”

从教程中,我认为rails会推断到“会话”的路由,我不需要为routes.rb添加规范路由。

如果我运行rake路线,我会得到以下

      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"}

我的routes.rb包含

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'

如果我将以下行添加到routes.rb,我可以让它工作,但我认为我不需要明确地执行此操作

  match '/sessions',:to => 'sessions#create'

我是否遗漏了某些东西或误解了什么? 我正在运行Rails 3.0.11和Ruby 1.9.2p290

1 个答案:

答案 0 :(得分:4)

在导轨中GET动词请求/sessions路由到index操作。

您需要浏览/sessions/new,或者像在问题的最后部分一样添加其他匹配。

这些是resourcehttp://guides.rubyonrails.org/routing.html#crud-verbs-and-actions

的默认路线