5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Railsでドメインごとにroutesを設定する

Posted at

サブドメインで切り替えるのはあるけどドメインごとに切り替える情報はあまりなかったのでメモ。

環境: Rails5.2.0, ruby2.5.1

routes.rb
Rails.application.routes.draw do
  constraints -> (req) { req.host == ENV['MAIN_HOST'] } do
    root 'dashboard#index'
    devise_for :users, path: 'users'

    resources :hoges
  end

  constraints -> (req) { req.host == ENV['ADMIN_HOST'] } do
    devise_for :admin_users, path: 'admin_users'

    mount RailsAdmin::Engine => '/rails_admin', as: 'rails_admin'

    scope module: 'admin_users' do
      root 'dashboard#index'
      resource :fugas
    end

  end

  mount LetterOpenerWeb::Engine, at: '/letter_opener' if Rails.env.development?
end

constraintsでHOSTを判定して使えるルートを切り替えています。
管理用のドメイン(ENV['ADMIN_HOST'])のコントローラーはcontrollers/admin_usersフォルダの配下に全てツッコミますが、url上は/admin_usersを見せなくても良いように、moduleを指定しています。

なので、dashboard_controllerはcontrollers/dashboard_controllerは.rbcontrollers/admin_users/dashboard_controllerは.rbの2つを用意しているような形です。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?