如何在帖子之间添加元素?

时间:2013-04-12 17:29:24

标签: html ruby-on-rails ruby-on-rails-3

我正在开发一个类似于博客的应用程序,我需要在每个页面中的三个帖子之后从帖子中添加不同的元素(广告)。为了更好地解释我想做什么,我将试图说明:

[ POST ]
[ POST ]
[ POST ]
[ ELEMENT]
[ POST ]
[ POST ]
[ POST ]
[ ... ]

在每个页面中,我都希望在不中断for周期而不使用任何Javascript或CSS hack的情况下遵循此模式,只需要服务器端技巧即可。

1 个答案:

答案 0 :(得分:2)

如何在每个上使用索引,并检查模数?

Posts.each_with_index do  |post,index| 
    [ Post _content_ here ]
   if index % 3 == 0
      [Element content here ] 
   end
end