我想在展示视图中显示最近的帖子,边栏中的下五个最新帖子.
目前,我显示了最新的帖子,但侧栏包含了相同的帖子以及接下来的4篇最新帖子.
控制器:
def show
@post = Post.find(params[:id])
@posts = Post.includes(:comments).order("created_at DESC").limit(5)
end
Run Code Online (Sandbox Code Playgroud)
视图:
<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-->
Run Code Online (Sandbox Code Playgroud)
非常感谢.