2024.08.14(更新日: 2024.08.14)
インスタンス変数の取説

はじめに
インスタンス変数はどうやって定義して、どうやって使うのか?
インスタンス変数の定義方法
コントローラに定義する。
以下の例では、Blogからすべてのデータを取得して、@blogsというインスタンス変数に代入している。

モデルクラス
Blogは、blogsテーブルのモデルクラスと考えられる。

インスタンス変数の使用方法
ビューファイル(app/views/posts/index.html.erbなど)で使用する。複数の場合は、以下のようにeach文を使用して、一つ一つ取り出して処理を行う。

投稿ID : 23965
コメントを残す