I dynamically create URLs of the form username.users.example.com
:
我动态创建username.users.example.com形式的URL:
bob.users.example.com
tim.users.example.com
scott.users.example.com
All of *.users.example.com
requests should go to a particular controller/action. How do I specify this in routes.rb
?
所有* .users.example.com请求都应转到特定的控制器/操作。如何在routes.rb中指定它?
All other requests to www.example.com
go to the normal list of routes in my routes.rb
file.
对www.example.com的所有其他请求都会转到routes.rb文件中的正常路由列表。
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):
更新:我观看有关子域的railscast,它显示了下面的代码,这似乎正是我需要的(更改了控制器和子域):
match '', to: 'my_controller#show', constraints: {subdomain: /.+\.users/}
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 '/*'
?
问题是它只匹配根URL。我需要这个以匹配每个可能的URL与* .users子域。显然我会把它放在我的routes.rb文件的顶部。但是如何指定一条全能路线呢?它只是'*'吗?要么 '/*'?
2 个解决方案
#1
7
I think, you just need to do the following :
我想,你只需要做以下事情:
create a class Subdomain
in lib
:
在lib中创建一个类子域:
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.host.include?('.users')
end
end
and in your routes
:
在你的路线:
constraints Subdomain do
match '', to: 'my_controller#show'
end
#2
-1
You can constraint route dynamically based on some specific criteria by creating a matches?
method
您可以通过创建匹配来根据某些特定条件动态约束路由?方法
Lets say we have to filter sub domain of URL
让我们说我们必须过滤URL的子域
constraints Subdomain do
get '*path', to: 'users#show'
end
class Subdomain
def self.matches?(request)
(request.subdomain.present? && request.subdomain.start_with?('.users')
end
end
What we are doing here is checking for URL if it start with sub domain users
then only hit users#show
action. Your class must have mathes?
method either class method or instance method. If you want to make it a instance method then do
我们在这里做的是检查URL,如果它从子域用户开始,然后只点击用户#show action。你的班级必须有数据?方法无论是类方法还是实例方法。如果你想让它成为一个实例方法,那么就做
constraints Subdomain.new do
get '*path', to: 'proxy#index'
end
you can achieve same thing using lambda
as well like below.
你可以使用lambda同样的东西,如下所示。
Instead of writing class we can also use lambdas
我们也可以使用lambdas而不是编写类
get '*path', to: 'users#show', constraints: lambda{|request|request.env['SERVER_NAME'].match('.users')}
#1
7
I think, you just need to do the following :
我想,你只需要做以下事情:
create a class Subdomain
in lib
:
在lib中创建一个类子域:
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.host.include?('.users')
end
end
and in your routes
:
在你的路线:
constraints Subdomain do
match '', to: 'my_controller#show'
end
#2
-1
You can constraint route dynamically based on some specific criteria by creating a matches?
method
您可以通过创建匹配来根据某些特定条件动态约束路由?方法
Lets say we have to filter sub domain of URL
让我们说我们必须过滤URL的子域
constraints Subdomain do
get '*path', to: 'users#show'
end
class Subdomain
def self.matches?(request)
(request.subdomain.present? && request.subdomain.start_with?('.users')
end
end
What we are doing here is checking for URL if it start with sub domain users
then only hit users#show
action. Your class must have mathes?
method either class method or instance method. If you want to make it a instance method then do
我们在这里做的是检查URL,如果它从子域用户开始,然后只点击用户#show action。你的班级必须有数据?方法无论是类方法还是实例方法。如果你想让它成为一个实例方法,那么就做
constraints Subdomain.new do
get '*path', to: 'proxy#index'
end
you can achieve same thing using lambda
as well like below.
你可以使用lambda同样的东西,如下所示。
Instead of writing class we can also use lambdas
我们也可以使用lambdas而不是编写类
get '*path', to: 'users#show', constraints: lambda{|request|request.env['SERVER_NAME'].match('.users')}