LoginSignup
0
0

Rails ルーティング namespace / module の使い方

Last updated at Posted at 2024-05-17

はじめに

わからなかったことを教えてもらったので、
アウトプットに投稿します!

初っ端から躓いたルーティング…
namespace / module はどう使い分けるのか😕
まとめていきます!

ルーティングの特徴

何も指定しない場合(通常のルーティング)

routes.rb
get 'home/about' => 'homes#about'
通常のルーティング
Prefix Verb URI Pattern Controller#Action
home_about GET /home/about(.:format) homes#about
routes.rb
resources :users, only: [:show, :index, :edit, :update] do
通常のルーティング
Prefix Verb URI Pattern Controller#Action
user GET /users/:id(.:format) users#show

namespace使用の場合

URLにもファイル構成にも、最初に /admin がつく!

routes.rb
namespace :admin do
  resources :customers, only: [:index, :show,  :edit, :update]
  get 'orders/:id' => 'orders#show'
end
namespaceのルーティング
Prefix Verb URI Pattern Controller#Action
edit_admin_customer GET /admin/customers/:id/edit(.:format) admin/customers#index
admin GET /admin/orders/:id(.:format) admin/orders#show

module使用の場合

URLには /public がつかないが
ファイル構成には最初に /public がつく!

routes.rb
scope module: :public do
  resources :addresses, only: [:index, :edit, :create, :update, :destroy]
  get 'home/about' => 'homes#top', as: "about"
end

moduleの記入方法

routes.rb
scope module: ~~ do
end

scopeと記載があるが、これがmoduleの記入方法!
scopeは適用されていないので注意!

namespaceのルーティング
Prefix Verb URI Pattern Controller#Action
addresses GET /addresses(.:format) public/addresses#index
about GET /home/about(.:format) public/home/td>

最後に

scopeについても後日追記予定です!

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