0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Railsのルーティング設定

Posted at

基礎の基礎だけど、なにがなんだか...

1. root to

アプリケーションのルート(ホームページ)を設定する際に使います。

基本的な書き方

root to: 'コントローラ名#アクション名'


トップページとしてfood_categories#indexを設定:

root to: 'food_categories#index'


2. get

指定されたHTTPリクエストメソッドGETに対してルートを設定します。通常、リソースを取得するために使います。

基本的な書き方

get 'パス', to: 'コントローラ名#アクション名', as: '名前付きルート'


特定のカテゴリの詳細ページを設定:

get 'food_categories/:id', to: 'food_categories#show', as: 'food_category'

上記の例では:

パス/food_categories/:idに対応。
コントローラfood_categoriesのshowアクションを実行。
名前付きルートfood_category_pathが自動で生成される。



3. post

HTTPリクエストメソッドPOSTに対してルートを設定します。通常、新しいデータの作成に使用されます。

基本的な書き方

post 'パス', to: 'コントローラ名#アクション名'


新しいカテゴリのデータを作成:

post 'food_categories', to: 'food_categories#create'


4. patch

HTTPリクエストメソッドPATCHに対してルートを設定します。既存データの部分更新に使用されます。

基本的な書き方

patch 'パス', to: 'コントローラ名#アクション名'


特定のカテゴリデータを部分更新:

patch 'food_categories/:id', to: 'food_categories#update'


## 5. put HTTPリクエストメソッドPUTに対してルートを設定します。既存データの全体更新に使用されます。

基本的な書き方

put 'パス', to: 'コントローラ名#アクション名'


カテゴリの全体更新:

put 'food_categories/:id', to: 'food_categories#update'


6. delete

HTTPリクエストメソッドDELETEに対してルートを設定します。データの削除に使用されます。

基本的な書き方

delete 'パス', to: 'コントローラ名#アクション名'


特定のカテゴリを削除:

delete 'food_categories/:id', to: 'food_categories#destroy'


7. リソースベースルーティング

CRUD操作(Create, Read, Update, Delete)を簡単に設定できる便利な方法です。

基本的な書き方

resources :リソース名

resources :food_categories
上記は以下を自動生成します:

HTTPメソッド パス コントローラ#アクション 名前付きルート
GET /food_categories index food_categories_path
GET /food_categories/new new new_food_category_path
POST /food_categories create food_categories_path
GET /food_categories/:id show food_category_path
GET /food_categories/:id/edit edit edit_food_category_path
PATCH/PUT /food_categories/:id update food_category_path
DELETE /food_categories/:id destroy food_category_path
特定のアクションのみを使いたい場合
resources :food_categories, only: [:index, :show]

特定のアクションを除外

resources :food_categories, except: [:destroy]


8. ネストルーティング

親リソースに関連する子リソースを扱う場合に使います。

基本的な書き方

resources :親リソース名 do
  resources :子リソース名
end


カテゴリごとのレシピを管理:

resources :food_categories do
  resources :recipes
end
HTTPメソッド パス コントローラ#アクション 名前付きルート
GET /food_categories/:food_category_id/recipes index food_category_recipes_path
GET /food_categories/:food_category_id/recipes/:id show food_category_recipe_path


9. カスタムルーティング

自由にルートを追加できます。

基本的な書き方

resources :リソース名 do
  collection do
    HTTPメソッド 'カスタムパス', to: 'コントローラ名#アクション名'
  end

  member do
    HTTPメソッド 'カスタムパス', to: 'コントローラ名#アクション名'
  end
end

resources :food_categories do
  collection do
    get 'search', to: 'food_categories#search' # 全体に対するアクション
  end
  member do
    get 'details', to: 'food_categories#details' # 特定のレコードに対するアクション
  end
end
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?