ruby-on-rails – Rails:显示5条最近的帖子,不包括最近的帖子

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails:显示5条最近的帖子,不包括最近的帖子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想在展示视图中显示最近的帖子,边栏中的下五个最新帖子.

目前,我显示了最新的帖子,但侧栏包含了相同的帖子以及接下来的4篇最新帖子.

控制器:

@H_403_6@def show @post = Post.find(params[:id]) @posts = Post.includes(:comments).order("created_at DESC").limit(5) end

视图:

@H_403_6@<div class="related-articles"> <h2 class="headline">Related Articles</h2> <% @posts.each do |post| %> <div class="floatLeft"><%= link_to (image_tag post.image.url(:thumb)),post_path(post) %></div> <h2 class="headline smaller-font"><%= link_to post.title,post %></h2> <div class="image-remove"><%= raw truncate_html(post.body,length: 190) %> <%= link_to "read more",post %></p></div> <hr> <% end %> </div><!--related articles Box-->

非常感谢.

解决方法

抵消是你想要的: @H_403_6@@posts = Post.includes(:comments).order("created_at desc").limit(4).offset(1)

这将返回帖子2-5,如果你想要2-6然后使用限制(5)

猜你在找的Ruby相关文章