我有两个需要显示帖子列表而不重复第一个块的块

时间:2015-04-24 07:40:47

标签: ruby-on-rails

我想显示一个2柱的列,只显示图像,其余的显示4列,显示其余的帖子而不重复前两个。不太清楚如何做到这一点。

class PostsController < ApplicationController
 def index
  # the 2 is what makes only 2 post show
  @posts = Post.most_hit(1.day.ago, 2)
  @all_posts = Post.most_hit(1.day.ago)
 end
end

<% @posts.each do |post| %>
 <div class="col-md-6">
  <%= image_tag attachment_url(post, :image) %>
 </div>
<% end %>

<% @all_posts.each do |post| %>
 <div class="col-md-3">
  <%= image_tag attachment_url(post, :image) %>
 </div>
<% end %>

1 个答案:

答案 0 :(得分:1)

好吧,考虑到你迟早需要分页,你宁愿在db级别这样做:

def index
  # the 2 is what makes only 2 post show
  @posts = Post.most_hit(1.day.ago, 2)
  @all_posts = Post.where.not(id: @posts.map(&:id)).most_hit(1.day.ago)
end

或者,如果您只想使用数组:

def index
  @all_posts = Post.most_hit(1.day.ago)
  @posts = @all_posts.slice!(0, 2)
end