[英]Rails: How to route to a specific domain
I have the following route defined: 我定义了以下路线:
constraints( subdomain: "abc" ) do
resources :sites do
resources :pages
end
resources :domains
end
So I can access a page like so: abc.example.com/sites/1/pages/2
因此,我可以访问如下页面:
abc.example.com/sites/1/pages/2
I want to route only the show
action of the PagesController
to be accessible like this: a-different-domain.com/2
我只希望将
PagesController
的show
动作PagesController
为可访问,如下所示: a-different-domain.com/2
I have many domains and one domain can have many pages. 我有很多域,一个域可以有很多页面。 Domains have the column
domain
. 域具有列
domain
。 This is what I've thought would work: 我认为这是可行的:
%w(domains).each do |d|
constraints( host: d.domain ) do
get "/*structure" => "pages/dashboard/sites/pages#show"
end
end
I am getting the following error: 我收到以下错误:
undefined method `domain' for "domains":String (NoMethodError)
未定义的“域”方法“域”:字符串(NoMethodError)
How can you do that with Rails? 如何使用Rails做到这一点?
The problem with your code is that %w(domains)
is ["domains"]
您的代码存在的问题是
%w(domains)
是["domains"]
You could do this: 您可以这样做:
# routes.rb
Domain.find_each do |d|
constraints(subdomain: d.domain) do
get "/*structure" => "pages/dashboard/sites/pages#show"
end
end
For more information: http://edgeguides.rubyonrails.org/routing.html#request-based-constraints 有关更多信息: http : //edgeguides.rubyonrails.org/routing.html#request-based-constraints
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.