我想为我的博客实现一个类似的设计,所以对于我在<%if @posts.first == post %> class="first-post" <%end%>
视图中的第一篇文章我现在要区分第二个5 7 ...帖子来自2 4 ..帖子我怎么能这样做?
答案 0 :(得分:0)
为清晰和简洁而编辑:
<% @posts.each_with_index do |p, index| %>
<% position = index+1 %>
<% if position == 1 then %>
FIRST
<% elsif (position.even?) then %>
EVEN
<% else %>
ODD
<% end %>
<% end %>
答案 1 :(得分:0)
您可以pop
数组中的第一个对象:
post = @posts.pop
然后你可以用它做你喜欢的事。也许是局部的。然后你可以循环其余的帖子。如果需要粒度控制,可以使用数组slice
:
<% @posts.each_slice(2) do |slice| %>
<div class="row">
<% slice.each do |post| %>
<div class="item">
show item here
</div>
<% end %>
</div>
<% end %>
这将以两个为一组拆分数组,使您可以更精细地控制布局。
或者,建议改进msanteler的答案:
<% @posts.each_with_index do |post, i| %>
<% if i == 0 %>
show stuff
<% elsif i.even? %>
show stuff
<% else %>
show stuff
<% end %>
<% end %>