我正在成功使用each_slice
-可以正常使用。但是,它还在页面底部显示数组内容,就像我在此处添加了puts
一样。
我有一个非常简单的控制器方法:
def index
@posts = Post.all
end
这里是视图:
.container
.row
.col-md-12{ style: "margin-top: 25px;" }
%h1#intro-h Posts
.row
.col-md-12
- @posts.each_slice(2) do |group|
.row{style: 'margin-bottom: 30px;'}
= group.each do |post|
.col-md-2
= image_tag post.user.profile_image.url, style: "height: 100px; border-radius: 50%;"
.col-md-4
%h3{style: 'margin-top: -5px;'}
= link_to post.title, post
Created by
= post.first_name
= post.last_name
%br
%p
.row
.col-md-4
= link_to "Read more", post, class: "btn btn-default", style: "margin-top: 8px; width: 100px;", method: :get
.col-md-4
= render 'paginator'
这将显示在页面底部:
[#<Post id: 3, title: "Test Post", user_id: 3, description: "Test Post", slug: "test-post", featured_image: nil>]
有什么想法要摆脱页面底部的数组显示?
答案 0 :(得分:0)
有问题的部分是= group.each do |post|
,因为each
除了运行循环外还返回数组本身。并且由于在其前面有一个=
符号,所以=
中的view
还将呈现group.each
语句的返回输出。您可能想将-
用作- group.each do |post|
,因为它将呈现group.each
的输出。