Skip to content

Instantly share code, notes, and snippets.

@Deanout
Created March 23, 2020 19:22
Show Gist options
  • Save Deanout/598a5eac48dbc6bedfdf3a8f48eea129 to your computer and use it in GitHub Desktop.
Save Deanout/598a5eac48dbc6bedfdf3a8f48eea129 to your computer and use it in GitHub Desktop.
Using in_groups_of to create 3 posts per row layout.
<div class="container">
<div class="row">
<div class="digg_pagination">
<%= will_paginate @posts, :container => false %>
</div>
</div>
<!-- Creates a loop of @posts.count / 3 arrays -->
<% @posts.in_groups_of(3).each do |post_array| %>
<div class="row">
<!-- For each post in the array, give it a class of "col-4" which is 3 per row-->
<% post_array.each do |post| %>
<!-- If posts.count = 4, last array will be [post4, nil, nil] so if it's nil, don't do anything or you'll get an error-->
<% unless post.nil? %>
<div class="col-4">
<div class="card mb-3">
<img class="card-img-top" src="<%= url_for post.optimized_image(post.thumbnail, 586, 180) %>" alt="Blog Post Image Card" />
<div class="card-body">
<h5 class="card-title">
<%= link_to post.title, post %>
</h5>
<p class="card-text">
<%= post.body.to_plain_text.truncate_words(50) %>
</p>
<p class="card-text">
<small class="text-muted">
<%= post.updated_at.strftime("%A, %B %e, %Y") %> by
<%= post.user.username %> | <%= pluralize(post.views, "View") %> | 0 comments
</small>
</p>
</div>
</div>
</div>
<!-- Ends the "unless post.nil" block -->
<% end %>
<!-- Ends the "post_array.each" block -->
<% end %>
</div>
<!-- Ends the "@posts.in_groups_of" block -->
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment