Ruby on Rails将通配符子域路由到控制器/操作

编程入门 行业动态 更新时间:2024-10-28 06:21:56
本文介绍了Ruby on Rails将通配符子域路由到控制器/操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我以 username.users.example 的形式动态创建URL:

I dynamically create URLs of the form username.users.example:

bob.users.example tim.users.example scott.users.example

所有 *。users.example 请求都应转到特定的控制器/操作。如何在 routes.rb 中指定?

All of *.users.example requests should go to a particular controller/action. How do I specify this in routes.rb?

对 www的所有其他请求。 example 转到我的 routes.rb 文件中的常规路由列表。

All other requests to www.example go to the normal list of routes in my routes.rb file.

更新 :我观看了有关子域的跟踪信息并且显示了以下代码,这似乎正是我需要的代码(更改了控制器和子域):

UPDATE: I watch the railscast about subdomains and it showed the following bit of code which would seem to be exactly what I need (changed the controller and subdomain):

match '', to: 'my_controller#show', constraints: {subdomain: /.+\.users/}

问题在于它仅与根URL匹配。我需要它来匹配每个可能的URL与 *。users 子域。所以很明显,我会将其放在 routes.rb 文件的顶部。但是,如何指定全部路线?是否只是‘*’

The problem is it only matches the root URL. I need this to match EVERY possible URL with a *.users subdomain. So obviously I would put it at the top of my routes.rb file. But how do I specify a catch-all route? Is it simply '*'? Or '/*'?

推荐答案

我认为,您只需要执行以下操作:

I think, you just need to do the following :

在 lib

create a class Subdomain in lib :

class Subdomain def self.matches?(request) request.subdomain.present? && request.host.include?('.users') end end

并在您的路线中:

constraints Subdomain do match '', to: 'my_controller#show' end

更多推荐

Ruby on Rails将通配符子域路由到控制器/操作

本文发布于:2023-10-26 10:51:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:通配符   路由   控制器   操作   Ruby

发布评论

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

>www.elefans.com

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