1
2

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.

Viewでのrenderの使い方

Last updated at Posted at 2016-12-18

部分テンプレートで、_memo.html.erbがある前提

_memo.html.erb:point_up:
<li>
  <%= memo.text %>
  <%= link_to '削除', memo_path(memo), method: :delete, remote: true %>
</li>

memo_path については、ルーティングについてを参照

その1

index.erb
<ul>
  <% @memos.each do |form| %>
    <%= render partial: "memo", locals: {memo: form} %>
  <% end %>
</ul>

memo は、_memo.html.erbで利用しているオブジェクト
memoオブジェクトにformオブジェクトを渡している

その2

index.erb
<ul>
  <% @memos.each do |form| %>
    <%= render "memo", memo: form %>
  <% end %>
</ul>

その1を簡略化したパターン

その3

これが良いらしい

index.erb
<ul>
  <%= render @memos %>
</ul>
  • 呼び出し側では、renderメソッドにコレクションを渡す(例:@memos
  • 呼び出される側では、ファイル名をコレクションの単数系にする(例:_memo.html.erb)
  • オブジェクトにはその単数系でアクセス可能。(例:memo.text)
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?