新手问题 使用 render partial 的 collection 来代替 each,是不是就无法加入 index 了?

QueXuQ · 2013年12月05日 · 最后由 QueXuQ 回复于 2013年12月05日 · 1994 次阅读
<%= @lista = News.all
    render :partial => "newsitem",
           :layout => "list_news",
           :collection => @lista
%>

是不是使用上面的情况来做循环,就用不到each_with_index的类似的效果了?

<partial_name>_counter

Ref

#1 楼 @xstmjh

Rails also makes a counter variable available within a partial called by the collection, named after the member of the collection followed by _counter. For example, if you're rendering @products, within the partial you can refer to product_counter to tell you how many times the partial has been rendered. This does not work in conjunction with the as: :value option.

根据这里说的不可以使用 as,但是我把 as 去掉了,还是不行。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号