我以 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 $ c $中创建类 Subdomain c>:
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将通配符子域路由到控制器/操作
发布评论