我正在开发一个类似于博客的应用程序,我需要在每个页面中的三个帖子之后从帖子中添加不同的元素(广告)。为了更好地解释我想做什么,我将试图说明:
[ POST ]
[ POST ]
[ POST ]
[ ELEMENT]
[ POST ]
[ POST ]
[ POST ]
[ ... ]
在每个页面中,我都希望在不中断for
周期而不使用任何Javascript或CSS hack的情况下遵循此模式,只需要服务器端技巧即可。
答案 0 :(得分:2)
如何在每个上使用索引,并检查模数?
Posts.each_with_index do |post,index|
[ Post _content_ here ]
if index % 3 == 0
[Element content here ]
end
end