はじめに
わからなかったことを教えてもらったので、
アウトプットに投稿します!
初っ端から躓いたルーティング…
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
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は適用されていないので注意!
Prefix | Verb | URI Pattern | Controller#Action |
---|---|---|---|
addresses | GET | /addresses(.:format) | public/addresses#index |
about | GET | /home/about(.:format) | public/home/td> |
最後に
scopeについても後日追記予定です!