使用渲染集合渲染树
我有一个有评论的网站。显示它们时,项目的第一个评论不应缩进,但后续的评论应缩进。
HTML 输出应类似于:
<ul>
<li>review for item 1</li>
<li>
review for item 2
<ul>
<li>2nd review for item 2</li>
<li>3rd review for item 2</li>
</ul>
</li>
</ul>
我得到的输出如下所示:
<ul>
<li>review for item 1</li>
<li>
review for item 2
</li> <!-- wrong -->
<li> <!-- wrong -->
<ul>
<li>2nd review for item 2</li>
<li>3rd review for item 2</li>
</ul>
</li>
</ul>
_reviews.html.erb
<%= render :partial => "review_item", :collection => @reviews, :locals => { :reviews_local => @reviews } %>
_review_item.html.erb
<% if review_item_counter == 0 %>
<ul>
<% end %>
<li>
<% if @current_reviewable != review_item.id %>
<% @current_reviewable = review_item.id %>
<%= review_item.body %>
<% else %>
<% reviews_local.slice!(review_item_counter, reviews_local.size) %>
<%= render :partial => "review_item", :collection => reviews_local, :locals => { :reviews_local => reviews_local } %>
<% end %>
</li>
<% if review_item_counter == reviews_local.size - 1 %>
</ul>
<% end %>
I have a site with reviews. When displaying them, the first review for an item should not be indented, but subsequent ones should be.
The HTML output should look something like:
<ul>
<li>review for item 1</li>
<li>
review for item 2
<ul>
<li>2nd review for item 2</li>
<li>3rd review for item 2</li>
</ul>
</li>
</ul>
The output I'm getting looks like this:
<ul>
<li>review for item 1</li>
<li>
review for item 2
</li> <!-- wrong -->
<li> <!-- wrong -->
<ul>
<li>2nd review for item 2</li>
<li>3rd review for item 2</li>
</ul>
</li>
</ul>
_reviews.html.erb
<%= render :partial => "review_item", :collection => @reviews, :locals => { :reviews_local => @reviews } %>
_review_item.html.erb
<% if review_item_counter == 0 %>
<ul>
<% end %>
<li>
<% if @current_reviewable != review_item.id %>
<% @current_reviewable = review_item.id %>
<%= review_item.body %>
<% else %>
<% reviews_local.slice!(review_item_counter, reviews_local.size) %>
<%= render :partial => "review_item", :collection => reviews_local, :locals => { :reviews_local => reviews_local } %>
<% end %>
</li>
<% if review_item_counter == reviews_local.size - 1 %>
</ul>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论