LoginSignup
0
0

More than 3 years have passed since last update.

CRUDについてのまとめ

Posted at

CRUD

アプリケーションのデータ取り扱いに関して、基本的な処理の頭文字を並べたものです。
アプリケーションの機能は、この4つの処理を組み合わせながら実装します。

C(Create)  R(Read)  U(Update)  D(Delete)
  生成      読み取り    更新       削除

Railsでは、CRUDを7つのアクションに分割して、処理を実現します。

7つのアクション

CRUDを実現するためには、それぞれの処理を記述する必要があります。
Ruby on Railsには、それらのアクションの設定が慣習的に決められており、下記の表のようなアクションが存在します。

アクション名  内容
index    一覧表示
show     詳細表示
new       生成
create    保存
edit      編集
update    更新
destroy   削除

これを本カリキュラムでは、「7つのアクション」と呼んでいます。
これらの7つのアクションのルーティングは、resourcesメソッドを使用することで一度に設定が可能です。

resourcesメソッド

resourcesは、7つのアクションへのルーティングを自動生成するメソッドです。
resourcesの引数に、:tweets というシンボルを指定すると/tweetsのパスに対応するルーティングが生成されます。

例】resourcesメソッドの使用例

Rails.application.routes.draw do
  resources :tweets
end

上記のように記載すると、以下のような7つのアクションすべてのルーティングを設定したことになります。

例】resourcesメソッドで自動生成されるルーティング

GET          /tweets(.:format)                   tweets#index
POST         /tweets(.:format)                   tweets#create
GET          /tweets/new(.:format)               tweets#new
GET          /tweets/:id/edit(.:format)          tweets#edit
GET          /tweets/:id(.:format)               tweets#show
PATCH        /tweets/:id(.:format)               tweets#update
PUT          /tweets/:id(.:format)               tweets#update
DELETE       /tweets/:id(.:format)               tweets#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