Rails Restful Routing and Subdomains

后端 未结 5 669
悲&欢浪女
悲&欢浪女 2020-12-28 17:57

I wondered if there were any plugins or methods which allow me to convert resource routes which allow me to place the controller name as a subdomain.

Examples:

5条回答
  •  -上瘾入骨i
    2020-12-28 18:47

    As ileitch mentioned you can do this without extra gems it's really simple actually.

    I have a standard fresh rails app with a fresh user scaffold and a dashboard controller for my admin so I just go:

    constraints :subdomain => 'admin' do
        scope :subdomain => 'admin' do
            resources :users
    
            root :to => "dashboard#index" 
        end
    end
    

    So this goes from this:

    • site.com/users

    to this :

    • admin.site.com/users

    you can include another root :to => "{controller}#{action}" outside of that constraint and scope for site.com which could be say a pages controller. That would get you this:

    constraints :subdomain => 'admin' do
        scope :subdomain => 'admin' do
            resources :users
    
            root :to => "dashboard#index" 
        end
    end
    
    root :to => "pages#index"
    

    This will then resolve:

    • site.com
    • admin.site.com
    • admin.site.com/users

提交回复
热议问题