Reputation: 846
I would like to create a dynamic routes, like this
https://subdomain.mysite.me/admin
https://mysite.me/subdomain/admin
I can set my routes
for subdomain
constraints, or namespace
, but I don't know how to make them both are available.
Upvotes: 4
Views: 962
Reputation: 1345
You just check this
I set up my rails app to use custom subdomains following this awesome RailsCast tutorial by Ryan Bates. Users can visit company.lvh.me:3000 and view all its relevant information. Now, I would like to add an admin subdomain on the front of the custom subdomain (admin.company.lvh.me:3000).
The idea is that I have specific admins for each blog with special controllers/views. So if admin is added to the front of the company subdomain, rails will route through the app/controllers/admin/blogs_controller.rb and show the app/views/admin/blogs/show.html.erb.
I've used the admin namespace and set it as a subdomain in some of my past Rails apps, but when I try that here I need the second subdomain to be dynamic:
namespace :admin, path: '/', constraints: { subdomain: 'admin.DYNAMIC' } do
match '/', to: 'blogs#show', via: 'get'
end
So if type admin.company in the subdomain constraint, it works like a charm, but how can make it dynamic? I've had no luck making it dynamic ('admin.' + Subdomain, etc.) in the routes files, which lead me to the thought: can I just use Ryan Bates's Subdomain class?
namespace :admin, path: '/', constraints: { subdomain: Subdomain } do
match '/', to: 'blogs#show', via: 'get'
end
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
Relevant Routes
Prefix Verb URI Pattern Controller#action
admin GET / admin/blogs#show
Upvotes: 1