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

QueXuQ · December 05, 2013 · Last by QueXuQ replied at December 05, 2013 · 1994 hits
<%= @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 去掉了,还是不行。

You need to Sign in before reply, if you don't have an account, please Sign up first.