を使って作業していますが、パーシャルとコレクションで問題が発生しました。Rails 3ネストしたパーシャル、コレクション、ローカル(レールチュートリアル第2版:第10章、エクササイズ5)
第10章、演習5の状態: "パーシャルを使用すると、リスト10.46とリスト10.47の削除リンクの重複がなくなります。
リスト10.46:アプリ/ビュー/共有/ _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>
私のアプローチでした:アプリ/ビュー/マイクロポスト/ _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がリストこのファイルを作成する 共有/ _item_delete_link.html.erb
<%= link_to "delete", item, method: :delete,
confirm: "You sure?",
title: item.content %>
そして、このようなオリジナルのパーシャルでこの部分を使用:10.46リスト
:アプリ/ビュー/マイクロポスト/ _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リスト:アプリ/ビュー/共有/ _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部分集合ごとにコレクション全体が再びレンダリングされていましたが、親部分で使用されていた元のコレクションのローカル変数を渡すだけでした。
レンダリングにはlocals: { }
とobject:
オプションを使用しようとしましたが、運がなかった。
誰でも回答が分かりますか?ありがとう!
ああ信じています<%=レンダリング部分: 'sh 'item:@ feed_item'と 'item:@micropost'を実行してみました。 '、しかし、私は、オブジェクトではなく、ローカル変数を渡す必要がありました。 duh –
'locals:'を使って正確なコードを表示してください。 Thx –
ちょうどそれをありがとう! –