Michael Hartl の Rails Tutorial 2nd Ed Chapter 10 Exercise 5に取り組んでいると、パーシャルとコレクション、およびパーシャル内でのパーシャルの使用に関する問題に遭遇しました。
第 10 章の演習 5 には、 「パーシャルを使用して、リスト 10.46 とリスト 10.47 の削除リンクの重複を排除します。」
リスト 10.46 : app/views/microposts/_micropost.html.erb
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= link_to "delete", micropost, method: :delete,
confirm: "You sure?",
title: micropost.content %>
<% end %>
</li>
リスト 10.47 : app/views/shared/_feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= link_to "delete", feed_item, method: :delete,
confirm: "You sure?",
title: feed_item.content %>
<% end %>
</li>
私のアプローチは、このファイルを作成することでした shared/_item_delete_link.html.erb
<%= link_to "delete", item, method: :delete,
confirm: "You sure?",
title: item.content %>
次に、このパーシャルを元のパーシャルで次のように使用します。
リスト 10.46 : app/views/microposts/_micropost.html.erb
<li>
<span class="content"><%= micropost.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(micropost.created_at) %> ago.
</span>
<% if current_user?(micropost.user) %>
<%= render partial: 'shared/item_delete_link', collection: @microposts, as: :item %>
<% end %>
</li>
リスト 10.47 : app/views/shared/_feed_item.html.erb
<li id="<%= feed_item.id %>">
<%= link_to gravatar_for(feed_item.user), feed_item.user %>
<span class="user">
<%= link_to feed_item.user.name, feed_item.user %>
</span>
<span class="content"><%= feed_item.content %></span>
<span class="timestamp">
Posted <%= time_ago_in_words(feed_item.created_at) %> ago.
</span>
<% if current_user?(feed_item.user) %>
<%= render partial: 'shared/item_delete_link', collection: @feed_items, as: :item %>
<% end %>
</li>
これですべてのテストに合格したので、ブラウザで確認するまでは機能していると思っていました: http://grab.by/daUk
_item_delete_link パーシャルごとにコレクション全体が再度レンダリングされますが、私が望んでいたのは、親パーシャルで使用された元のコレクションからローカル変数を渡すことでした。
renderのlocals: { }
およびオプションを使用してみましたが、うまくいきませんでした。object:
誰も答えを知っていますか?ありがとう!