Steve
Steve

Reputation: 89

Rails each_slice displaying array at bottom of page

I am using each_slice successfully - it works as expected. However, it is also displaying the array contents at the bottom of the page, as if I've added a puts there.

I have a very simple controller method:

def index
  @posts = Post.all
end

Here's the view:

.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'

And this gets displayed at the bottom of the page:

[#<Post id: 3, title: "Test Post", user_id: 3, description: "Test Post", slug: "test-post", featured_image: nil>]

Any idea how to get rid of that array display at the bottom of the page?

Upvotes: 0

Views: 99

Answers (1)

Minato
Minato

Reputation: 4533

The problematic part is = group.each do |post| since each returns the array itself as well in addition to running the loop. And since you have an = sign before it so the = in the view would also render the returned output of the group.each statement. You may want to use - as - group.each do |post| as it will render the output of group.each.

Upvotes: 1

Related Questions