设计路由:有没有办法从 Rails.application.routes 中删除路由? [英] Devise routing: is there a way to remove a route from Rails.application.routes?

查看:17
本文介绍了设计路由:有没有办法从 Rails.application.routes 中删除路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

devise_for 创建的路由包括我们想要删除的 DELETE 路由,并且 devise_for 不支持 :except>:only 选项.

devise_for creates routes including a DELETE route, which we want to remove, and devise_for doesn't support an :except or :only option.

如何从 Rails.application.routes 中删除路由?是在 draw 块中,还是在之后?

How can I remove a route from Rails.application.routes? Either in the draw block, or afterward?

这里是一个错误的详细信息,这是我们需要删除路由的原因.

Here are details of a bug, which was the reason we needed to remove the route.

  • 我们正在向自定义 UJS 控制器操作发出 DELETE 请求

  • we were issuing a DELETE request to a custom UJS controller action

在控制器操作中,我们删除了我们想要的内容,然后进行了 302 重定向.这是一个坏主意,后来我们通过返回一些 JSON 来纠正它.

in the controller action we were removing what we wanted to, then doing a 302 redirect. This was a bad idea, and we have since corrected it by returning some JSON instead.

此错误已修复,但我仍想删除该路线.

This bug has been fixed, but i would like to remove the route nonetheless.

这是我最后所做的,这是赏金获得者在 Jose´ Valim 的引述中建议的:

Here is what I did in the end, which was suggested by the bounty-winner in his quote from Jose´ Valim:

config/routes.rb 中,我在 devise_for 调用上面添加了这个,它设置了我的其余 'people' 路由:

In config/routes.rb, I added this above the devise_for call, which sets up the rest of my 'people' routes:

delete '/person', :to => 'people#destroy'

然后在我现有的 people_controller.rb 中,我添加了一个 no-op 方法:

Then in my existing people_controller.rb, I added a no-op method:

def destroy
  render :nothing => true
end

我仍然有点恼火,没有一种简单的方法可以从 RouteSet 中删除路由.此外,设计控制器的 delete 路由仍然存在,但它不会被调用,因为 rails 在 config/routes.rb 中查找第一个匹配项并返回它.

I'm still a little irked that there isn't a simple way to just remove the route from the RouteSet. Also, the delete route still exists for the devise controller, but it won't get called because rails looks for the first match in config/routes.rb and returns it.

推荐答案

这里是 Jose Valim(设计的作者)不得不说这个话题:

Here is what Jose Valim (the author of devise) has to say on the subject:

无法单独删除路由.或者你使用 :skip to删除所有并手动绘制您需要的那些,或者您覆盖它通过首先在配置中定义到相同路径的路由来路由路线.rb

There is no way to remove routes individually. Or you use :skip to remove all and draw the ones you need manually or you overwrite this routes by defining a route to the same path first in your config/ routes.rb

因此,对您的问题的简短回答是否定的,您不能删除那条路线.您当然可以尝试做一些诸如修补 devise_for 方法之类的事情,但这将是一项有点复杂的工作(一天或数天的努力).我只是使用 :skip 选项,然后为该控制器实现您想要的路由,而忽略您不需要的路由.

So the short answer to your question is no, you can't delete that one route. You can of course try doing things like patching the devise_for method, but that would be a somewhat involved undertaking (a day or several worth of effort). I'd just use the :skip option, then implement the routes you do want for that controller and leave off the one that you don't.

这篇关于设计路由:有没有办法从 Rails.application.routes 中删除路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆