Rails路由,URL和子域 [英] Rails routes, url and subdomains

查看:78
本文介绍了Rails路由,URL和子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ruby应用程序划分为不同的命名空间. 例如:免费(free.domain.com),专业(p​​ro.domain.com),贵宾(vip.domain.com) 在路由文件中如下所示:

My ruby app is divided in different namespaces. like: free(free.domain.com), pro(pro.domain.com), vip(vip.domain.com) In the routes file looks like this:

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.

I would like that wherever I am in the app when there is a link like pro_home_path, the url be pro.domain.com/home.

因此,基本上,是否可以在路由文件中添加一个子域(类似于namespace :vip, :subdomain => vip do这样的子域),以将该子域附加到相应的名称空间?

So basically, is it possible in the routes file to add a subdomain(something like this namespace :vip, :subdomain => vip do) to append the subdomain to the corresponding namespace?

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

但是当我执行pro_home_path时,链接是lvh.me/3000/pro/home而不是pro.lvh.me:3000/home

But the link when I do pro_home_path, I'm getting lvh.me/3000/pro/home instead of pro.lvh.me:3000/home

推荐答案

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

Yes, you can specify a subdomain as a constraint, e.g.

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

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

Check out the rails guide on the subject: http://guides.rubyonrails.org/routing.html#request-based-constraints

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

To link to a specific subdomain, you can specify it in a URL route helper. For example, home_url(subdomain: 'pro') might redirect to http://pro.example.com/home. Take care to use the _url suffixed methods as home_path will not redirect to a specific subdomain.

这篇关于Rails路由,URL和子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆