0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails のRouterについて

0
Last updated at Posted at 2021-03-07

Routerについて個人的にまとめます。
*訂正などありましたらぜひよろしくお願いします。

Router

まず書かれているファイルは
config/routes.rb

*ここではmessageとしています。



Rails.application.routes.draw do
  # CRUD
  get 'messages/:id', to: 'messages#show'
  post 'messages', to: 'messages#create'
  put 'messages/:id', to: 'messages#update'
  delete 'messages/:id', to: 'messages#destroy'

  # index: show の補助ページ
  get 'messages', to: 'messages#index'

  # new: 新規作成用のフォームページ
  get 'messages/new', to: 'messages#new'

  # edit: 更新用のフォームページ
  get 'messages/:id/edit', to: 'messages#edit'
end

これらを簡単に記述したのが、↓
root to: 'messages#index'はトップページにアクセスする時の記述
resources :messages, except: [:index]expect:を使うことで指定したアクションを外すことができる。

resources :コントローラー名

Rails.application.routes.draw do
  root to: 'messages#index' 

  resources :messages
end

(index, show, new, create, edit, update, destroy)これらをRESETfulなルーティングと言う。

Router の確認

rails roues


   Prefix Verb   URI Pattern                  Controller#Action
        root GET    /                            messages#index
    messages GET    /messages(.:format)          messages#index
             POST   /messages(.:format)          messages#create
 new_message GET    /messages/new(.:format)      messages#new
edit_message GET    /messages/:id/edit(.:format) messages#edit
     message GET    /messages/:id(.:format)      messages#show
             PATCH  /messages/:id(.:format)      messages#update
             PUT    /messages/:id(.:format)      messages#update
             DELETE /messages/:id(.:format)      messages#destroy
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?