Rails路由,URL和子域

时间:2014-02-03 20:23:57

标签: ruby-on-rails namespaces routes subdomain

我的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这样的链接,网址都是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

1 个答案:

答案 0 :(得分:9)

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

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

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

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