LoginSignup
470
456

More than 3 years have passed since last update.

Railsのルーティングの種類と要点まとめ

Last updated at Posted at 2016-05-20

はじめに

scopeやnamespaceを使用してルーティングを設定する機会があった。
これまでresoucesくらいの基礎的なルーティング設定しかしたことがなく、色々調べたのでまとめてみる。

目次

  • resource
  • namespace
  • scopeとmodule
  • concerns
  • member
  • collection
  • shallow(オプション)
  • as(オプション)
  • params(オプション)

resource

resourceは単数形リソースと呼ばれる。
場合によっては、ユーザーがページを表示する時にidを参照することのないリソースが使用されることがある。たとえば、マイページ(ここでは/profileとする)のように「現在ログインしているユーザー自身」のプロファイルを表示し、他のユーザーidを参照する必要がない場合には、単数形リソースを使用してshowアクションに (/profile/:idではなく) /profileを割り当てることができる。

resource :user

namespace

namespaceはコントローラを名前空間によってグループ化することができる。
どういうことかというと、ユーザーが使う部分とと管理者が使う部分をそれぞれ分けて管理できるようになる。管理者用のコントローラはcontrollers/adminディレクトリ以下に作成して管理するみたいに。

namespace :admin do
  resources :users, :tweets
end

ちなみに、controllers/admin以下にコントローラを作成するコマンドは以下

rails g controller admin/users

scope

もし、URLは/tweetsだけど、Admin::TweetsController(adminディレクトリ以下にコントローラを作成するとこういうクラス名になる)にルーティングしたいなんて場合に使う。

scope module: 'admin' do
  resources :tweets
end

#ブロックを使わない書き方もできる
resources :posts, module: 'admin'

逆に、/admin/tweetsというURLでTweetsControllerにルーティングしたい場合は、以下。

scope '/admin' do
  resources :tweets
end

#または
resources :tweets, path: '/admin/tweets/

concern

concernを使うと、他のリソースやルーティング内で使いまわせる共通のルーティングを宣言できる。

#使いまわしたいルーティングを定義
concern :commentable do
  resources :comments
end

concern :image_attachable do
  resources :images, only: :index
end

#concernで定義したルーティングを使い回す
resources :messages, concerns: :commentable
resources :posts, concerns: [:commentable, :image_attachable]

上のコードは以下と同じ。

resources :messages do
  resources :comments
end

resources :posts do
  resources :comments
  resources :images, only: :index
end

member

memberはメンバールーティング(photos/:id/previewのようにidを伴うパス)を追加するときに使う。
memberブロックをリソースブロック(resources :~~ do)の中に1つ追加する。追加したブロック内にメンバルーティングを記述する。

resources :photos do
  member do
    get :preview
  end
end

#追加したいメンバルーティングが1つならonオプションを使うと1行でいける
resources :photos do
  get :preview, on: :member
end

collection

ルーティングにコレクション(/photos/searchのようにidを伴わないパス)を追加するときに使う。

resources :photos do
  collection do
    get :search
  end
end

# こちらもonオプションで1行に
resources :photos do
  get :search, on: :collection
end

shallow

ネストが深くなったときに、生成するルーティングをいい感じにしてくれる。

resources :posts do
  resources :comments, shallow: true
end

#ネストするリソースが複数ある場合ならこっちのが良さげ
resources :posts, shallow: true do
  resources :comments
  resources :quotes
  resources :drafts
end

asオプション

:asオプションを使うと、ルーティングに名前を指定できる。

get 'exit', to: 'session#destroy', as: :logout

matchとviaオプション

matchメソッドと:viaオプションを使うと、複数のHTTPメソッドに同時にマッチするルーティングを作成できる。

match 'photos', to: 'photos#show', via: [:get, :post]

paramオプション

resources :postsというようなルーティング定義すると、#showなどのパスは:idとなる。
しかし、uidで検索する場合などは:uidとなっているべき。
以下のようにすると、:id以外の名前もつけることができる、

resources :posts, params: :uid

参考

http://railsguides.jp/routing.html
resources を nest するときは shallow を使うと幸せになれる - Qiita

疑問

_pathヘルパーには、それぞれに対応する_urlヘルパーがある。_urlヘルパーは、_pathの前に現在のホスト名、ポート番号、パスのプレフィックスが追加されている点が異なるようだが、使用上の違いがよくわからない。。。
コメントで教えていただけると嬉しいです!

回答

上記の疑問に対する回答がこちら。
コメントにて回答いただいた @kuboon さんありがとうございました!

url ヘルパの使いどころ

  • meta や ogp ではフルURLを書かないといけない決まりがある
  • メール本文の中
  • SNS 等にリンクを投げる時
  • redirect_to に渡すパラメタ (pathでも動作するが、HTTPの仕様上フルURLが必要なので、pathを渡すとrails内でフルURLに変換される)
470
456
3

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
470
456