我的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路线上,我不应该有[删除]而不是[GET]吗?如果是这样,我该怎么改呢?
答案 0 :(得分:5)
通过直接从浏览器导航到该地址,您实际上是在执行GET请求。默认情况下,浏览器会在用户输入URL并要求其呈现页面时提交GET请求。
您应该使用#link_to
在视图中插入一个链接,并将DELETE
方法作为选项传递,如下所示:
link_to "Signout", signout_path, :method => :delete