How to dynamically switch between a subdomain and a namespace in rail routes

I would like to create dynamic routes like this

https://subdomain.mysite.me/admin
https://mysite.me/subdomain/admin

I can set limits routesfor subdomainor namespace, but I don't know how to make them available.

+4
source share
1 answer

You just check this

I installed the rails app to use custom subdomains after this amazing RailsCast tutorial by Ryan Bates. Users can visit company.lvh.mehaps000 and view all relevant information. Now I would like to add the admin subdomain on the front of the user subdomain (admin.company.lvh.mehaps000).

, /. , , //admin/blogs_controller.rb /views/admin/blogs/show.html.erb.

admin Rails, , , :

namespace :admin, path: '/', constraints: { subdomain: 'admin.DYNAMIC' } do
  match     '/',            to: 'blogs#show', via: 'get'
end

, admin.company , , ? ( "admin." + Subdomain ..) , : ?

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
+1

Source: https://habr.com/ru/post/1664880/


All Articles