Rails路由,URL和子域

我的ruby应用程序分为不同的命名空间。 喜欢:free(free.domain.com),pro(pro.domain.com),vip(vip.domain.com)在routes文件中如下所示:

namespace :free do match 'home' => 'free#home', :via => [:get, :post], :as => :home #more routes end namespace :pro do match 'home' => 'pro#home', :via => [:get, :post], :as => :home #more routes end namespace :vip do match 'home' => 'vip#home', :via => [:get, :post], :as => :home #more routes end match '/about' => 'pages#about' match '/team' => 'pages#team' match '/press' => 'pages#press' #more routes 

我希望无论我在应用程序中有什么链接,如pro_home_path,url都是pro.domain.com/home。

所以基本上,在路由文件中是否可以添加子域(类似于此namespace :vip, :subdomain => vip do )将子域追加到相应的命名空间?

编辑:所以我添加了constraint constraints(:subdomain => "newsfeed") do

但是当我做pro_home_path时的链接,我得到的是lvh.me/3000/pro/home而不是pro.lvh.me:3000/home

是的,您可以将子域指定为约束,例如

 get 'photos', constraints: {subdomain: 'admin'} 

查看有关主题的rails指南: http : //guides.rubyonrails.org/routing.html#request-based-constraints

要链接到特定子域,可以在URL路由助手中指定它。 例如, home_url(subdomain: 'pro')可能会重定向到http://pro.example.com/home 。 注意使用_url后缀方法,因为home_path不会重定向到特定的子域。