0

我的 routes.rb 文件有以下内容:

Hchq::Application.routes.draw do
  resources :users
  resources :sessions, only: [:new, :create, :destroy]

  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout', to: 'sessions#destroy', via: :delete
end

当我跑步时,rake routes我得到:

      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
            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
     signup        /signup(.:format)         users#new
     signin        /signin(.:format)         sessions#new
    signout DELETE /signout(.:format)        sessions#destroy

然而,当我去时,http://localhost:3000/signout我得到以下信息:

路由错误:动作控制器异常

在日志中我得到:ActionController::RoutingError (No route matches [GET] "/signout"):

哪个,我不应该在 /signout 路由上使用 [DELETE] 而不是 [GET] 吗?如果是这样,我该如何更改?

4

1 回答 1

5

通过直接从浏览器导航到该地址,您实际上是在执行 GET 请求。默认情况下,当用户输入 URL 并要求它呈现页面时,浏览器会提交 GET 请求。

您应该在视图中插入一个链接,并使用该方法作为选项#link_to传递,如下所示:DELETE

link_to "Signout", signout_path, :method => :delete
于 2012-06-04T19:19:49.547 回答