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
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.
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?
EDIT: So I have added a constraint constraints(:subdomain => "newsfeed") do
But the link when I do pro_home_path, I'm getting lvh.me/3000/pro/home instead of pro.lvh.me:3000/home
TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.
Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.
Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.
Yes, you can specify a subdomain as a constraint, e.g.
get 'photos', constraints: {subdomain: 'admin'}
Check out the rails guide on the subject: http://guides.rubyonrails.org/routing.html#request-based-constraints
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With