我想显示一个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 %>
答案 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