1
0

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.

[Rails6]サブドメインでルーティングしたいという話

Posted at

https://hogehoge.com/:role/:controller/:action
みたいなURLが嫌になったので
https://role.hogehoge.com/:controller/:action
を目指す。

基本的な参考記事: Railsでサブドメインに対応させる方法
でもこのルーティングはまだちょっと不満だったので(コントローラの階層化もしたいしそれぞれのindexも作りたい)以下ちょっとそのあたりを拡充した話になります。

config/environments/development.rb
# ※めんどくさかったのでやっていませんが環境変数使うなりなんなりもうちょっといいやり方があると思います
# (略)
  config.hosts << "example.com"
  config.hosts << "worker.example.com"
  config.hosts << "admin.example.com"
# (略)
routes.rb
  constraints subdomain: '' do
    get '/', to: "pages#index"
  end
  constraints subdomain: 'worker' do
    scope module: :worker do # moduleにすることでドメイン後にworkerとか入らずに済む
      get '/', to: "pages#index"
    end
  end
  constraints subdomain: 'admin' do
    scope module: :admin do
      get '/', to: "pages#index"
    end
  end
app/controllers/admin/pages_controller.rb
class Admin::PagesController < ApplicationController
  layout 'admin' # おまけでレイアウトファイル振り分け
  def index
  end
end 

これで http://admin.example.com/:controller/:action ができました!やったね!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?