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?

More than 1 year has passed since last update.

数日間わからなかったcollectionオプションがやっと理解できた!

0
Last updated at Posted at 2023-08-10

部分テンプレートを呼び出す際にcollectionオプションを使った。

しかし、インスタンス変数(@prototype)から取り出した要素 (prototype)を部分テンプレート内で扱うことができる(取り出される前のインスタンス変数の状態では使えない)ということを、私はちゃんと理解できていなかったため、インスタンス変数の状態で呼び出すファイルを記入していたため、image nilのエラー画面が出ていた。

・もし、別の名前として取り出した値を使いたければ as:を使う。

<%= render partial: 'circle', collection: @circles, as: "square" %>

partialを記さないとエラーになるから注意する。
この時、部分テンプレ内の変数squareに@circlesが代入される

・renderメソッドにlocalsオプションをつけると、その部分テンプレートで使用する変数を定義することができる。

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?