本文介绍了会话路由#destroy action的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样链接到 Sessions 控制器的销毁操作:

I'm linking to the destroy action for the Sessions controller like this:

<%= link_to "Sign out", session_path, method: :delete  %>

Routes.rb:

resources :sessions, only: [:new, :create, :destroy]

Rails 抱怨上面的链接:

Rails complains about the link above:

没有路由匹配 {:action=>"destroy", :controller=>"sessions"} 缺少必需的键:[:id]

当没有为链接提供对象 ID 时,如何链接到销毁操作并在 Rails 中保留 REST/资源方法?

How do I link to the destroy action and keeping the REST/resource methodology in Rails when there's no object ID to provide for the link?

推荐答案

最好将到会话控制器的路由视为单一资源

It is best to treat the routes to your sessions controller as a singular resource

routes.rb

resource :sessions

文档:http://guides.rubyonrails.org/routing.html#singular-资源

这将为您提供一条无需 ID 即可使用的路线

This will give you a route that you can use without ID's

删除/sessions 会话#destroy

DELETE /sessions sessions#destroy

这篇关于会话路由#destroy action的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 00:07