LoginSignup
573
517

More than 5 years have passed since last update.

Railsのroutingにおけるscope / namespace / module の違い

Posted at

はじめに

以前書いた、下記の続き。
https://qiita.com/ryosuketter/items/1d9b620fa2508cfda37b

今回は、Railsのroutingにおけるscope / namespace / module の違いについて書きます。

結論

結論をまとめると

URL ファイル構成
scope 指定のパスにしたい 変えたくない
namespace 指定のパスにしたい 指定のパスにしたい
module 変えたくない 指定のパスにしたい

以下、詳細を説明します。

scope

以下の場合はscopeが望ましい

  • URLは指定のパスにしたい
  • ファイル構成変えたくない
config/routes.rb
Rails.application.routes.draw do
  scope :blog do
    resources :articles
  end
end

scope / routing

Helper HTTP Verb Path Controller#Action
articles_path GET /blog/articles(.:format)

articles#index

POST /blog/articles(.:format)

articles#create

new_article_path GET /blog/articles/new(.:format)

articles#new

edit_article_path GET /blog/articles/:id/edit(.:format)

articles#edit

article_path GET /blog/articles/:id(.:format)

articles#show

PATCH /blog/articles/:id(.:format)

articles#update

PUT /blog/articles/:id(.:format)

articles#update

DELETE /blog/articles/:id(.:format)

articles#destroy

scope / ファイル構成

スクリーンショット 2019-03-17 22.50.29.png

scope / controller

app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
  ...
end

そのままですね。

namespace

以下の場合はnamespaceが望ましい

  • URLは指定のパスにしたい
  • ファイル構成も指定のパスにしたい
config/routes.rb
Rails.application.routes.draw do
  namespace :blog do
    resources :articles
  end
end

namespace / routing

Helper HTTP Verb Path Controller#Action
blog_articles_path GET /blog/articles(.:format)

blog/articles#index

POST /blog/articles(.:format)

blog/articles#create

new_blog_article_path GET /blog/articles/new(.:format)

blog/articles#new

edit_blog_article_path GET /blog/articles/:id/edit(.:format)

blog/articles#edit

blog_article_path GET /blog/articles/:id(.:format)

blog/articles#show

PATCH /blog/articles/:id(.:format)

blog/articles#update

PUT /blog/articles/:id(.:format)

blog/articles#update

DELETE /blog/articles/:id(.:format)

blog/articles#destroy

namespace / ファイル構成

スクリーンショット 2019-03-17 22.29.06.png

namespace / controller

app/controllers/blog/articles_controller.rb
class Blog::ArticlesController < ApplicationController
  ...
end

Blog::が接頭辞ぽく付きましたね。

module

以下の場合はmoduleが望ましい

  • URLは変えたくない
  • ファイル構成だけ指定のパスにしたい
config/routes.rb
Rails.application.routes.draw do
  scope module: :blog do
    resources :articles
  end
end

module / routing

Helper HTTP Verb Path Controller#Action
articles_path GET /articles(.:format)

blog/articles#index

POST /articles(.:format)

blog/articles#create

new_article_path GET /articles/new(.:format)

blog/articles#new

edit_article_path GET /articles/:id/edit(.:format)

blog/articles#edit

article_path GET /articles/:id(.:format)

blog/articles#show

PATCH /articles/:id(.:format)

blog/articles#update

PUT /articles/:id(.:format)

blog/articles#update

DELETE /articles/:id(.:format)

blog/articles#destroy

module / ファイル構成

スクリーンショット 2019-03-17 22.29.06.png

module / controller

app/controllers/blog/articles_controller.rb
class Blog::ArticlesController < ApplicationController
  ...
end

こちらも、Blog::が接頭辞ぽく付きましたね。

以上です!

参考

573
517
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
573
517