Rails 4
我有15个帖子显示在数组列表<% @posts.each do |post| %>中。我想插入我的广告图像后的前3个帖子。不是每3个帖子。我怎样才能做到这一点呢?
我一直在看这个http://ruby-doc.org/core-2.2.2/Enumerable.html,但不知道哪一个适合我的问题。
发布于 2016-03-08 19:23:50
您可以尝试这样做:
<% @posts.each_with_index do |post,i|%>
<%if i == 2 %>
#your image here
<%end%>
<%end%>发布于 2016-03-08 19:27:13
尝试将each与索引一起使用
<%@posts.each.with_index(1) do |post, index| %>
<% if index==3 %>
your code to insert image
<%end%>发布于 2016-03-08 19:29:52
在第三个帖子之后,它会添加一个
`<% @posts.each_with_index do |post,index| %>
<% if index == 3 %>
<%= place_add_here %>
<% end %>
<% end %>`https://stackoverflow.com/questions/35865939
复制相似问题