如何在 Ruby on Rails 中创建一个包罗万象的路由?

编程入门 行业动态 更新时间:2024-10-10 05:26:19
本文介绍了如何在 Ruby on Rails 中创建一个包罗万象的路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我希望所有满足特定约束的请求都转到特定控制器.所以我需要一个包罗万象的路线.我如何在 Rails 中指定它?是这样的吗?

I want to have all requests that satisfy a certain constraint to go to a specific controller. So I need a catch-all route. How do I specify that in Rails? Is it something like this?

match '*', to: 'subdomain_controller#show', constraints: {subdomain: /.+.users/}

那真的能捕获所有可能的路线吗?重要的是,即使有很多嵌套目录,也不能漏掉.

Will that really catch all possible routes? It's important that none slip through even if there are many nested directories.

使用 Ruby on Rails 3.2,但准备升级到 4.0.

Using Ruby on Rails 3.2, but ready to upgrade to 4.0.

UPDATE:'*path' 似乎有效.但是,我遇到的问题是,只要文件存在于我的 public 目录中,Rails 就会呈现它.

UPDATE: '*path' seems to work. However, the issue I'm running into is whenever the file exists in my public directory, Rails renders that instead.

推荐答案

我认为您需要对这种方法稍作调整,但您明白了:

I think you need minor tweaks in this approach but you get the point:

更新:

#RAILS 3 #make this your last route. match '*unmatched_route', :to => 'application#raise_not_found!' #RAILS 4, needs a different syntax in the routes.rb. It does not accept Match anymore. #make this your last route. get '*unmatched_route', :to => 'application#raise_not_found!'

class ApplicationController < ActionController::Base ... #called by last route matching unmatched routes. #Raises RoutingError which will be rescued from in the same way as other exceptions. def raise_not_found! raise ActionController::RoutingError.new("No route matches #{params[:unmatched_route]}") end ... end

更多信息在这里:gist.github/Sujimichi/2349565

更多推荐

如何在 Ruby on Rails 中创建一个包罗万象的路由?

本文发布于:2023-11-28 08:48:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641728.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:包罗万象   路由   创建一个   如何在   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!