Struggling with aligning rows containing three divs each in Rails and bootstrap

Looking to display 3 article divs in each row on my website using bootstrap 3 grid system:

<div class="container-fluid">
<h1>NEWS</h1>

  <div class="row">

      <% @articles.each do |article| %>

        <div class="col-sm-4">
          <h2><%= article.title %></h2> 
          <p><strong>Author: </strong>
            <% article.author_id do |c| %>
              <p><%= c.first_name %></p>
            <% end %></p> 
          <p><strong>Excerpt: </strong><%= article.excerpt %></p> 

          <p><strong>Category: </strong>
            <% article.categories.each do |c| %>
              <%= c.name %>
            <% end %>
          </p> 

          <p><strong>Nr of comments: </strong><%= article.comments.count %></p> 

            <%= link_to article do %>
              <button type="button" class="btn btn-primary btn-sm">Show
              </button>
            <% end %>
            <%= link_to edit_article_path(article) do %>
              <button type="button" class="btn btn-success btn-sm">Edit
              </button>
            <% end %>
            <%= link_to article, method: :delete, data: { confirm: 'Are you sure?' }  do %>
              <button type="button" class="btn btn-danger btn-sm">Delete
              </button>
            <% end %>

        </div>
    <% end %>

          </div> <!-- .row -->

    </div> <!-- .container -->
<% content_for :aside do %>
  <%= render 'sidebar_popular' %>
  <%= render 'sidebar_categories' %>
<% end %>

The first row is displaying correctly but the second row only shows two divs (see image)

How do I fix this? I attempted inserting but it didn't make any difference (maybe placed incorrectly).

Answer №1

It appears that all the columns are being rendered in a single row. To display three columns in each row, you should modify the code to begin a new row after every third column:


  <% @articles.each_slice(3).to_a.each do |chunk| %>
     <div class="row">
       <% chunk.each do |article| %>
         <div class="col-sm-4">
           ...
         </div>
       <% end %>
    </div>
  <% end %>

The to_a method may not be necessary in the initial loop

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Position a modal in the vertical center with scroll functionality for handling extensive content

Looking for ways to tweak a Modal's CSS and HTML? Check out the code snippets below: div.backdrop { background-color: rgba(0, 0, 0, 0.4); height: 100%; left: 0; overflow: auto; position: fixed; top: 0; width: 100%; z-index: 2020; } ...

JQuery Anchor Link Scrolling Problem on Specific Devices

I'm having an issue where my webpage does not scroll correctly to an anchor when a link is clicked. The problem arises from the header size changing based on the viewport width. While it works fine in desktop Chrome, in the mobile version the header h ...

"Interactive Connect 4 Game in Javascript - Drop the disk into the bottom row of the chosen

Check out this awesome Connect4 game I found: http://codepen.io/anon/pen/lmFJf I have a specific goal in mind for the game. When I click on an empty space in any column, I want it to automatically drop into the lowest available spot in that column, follow ...

Guide to incorporating flash into a form similar to the method used on http://www.mediafire.com

I'm looking to integrate a flash upload feature into my index file, but I'm not sure how to do it. Here is the link to the flash uploader: . I want it to work similar to how uploading works on when you click on UPLOAD. Thank you for any assistan ...

What is the best way to center a Checkbox in HTML5 and CSS?

I've been struggling to center my Checkbox with label on my current website project. Despite searching online for solutions, I'm still unable to find the right method! If you're curious, you can check out the source code of my webpage at ( ...

Endless cycle within the while loop without any obvious cause

I've been tinkering with a timer and thanks to some feedback I received in this community, everything is running smoothly. Here's what the current version looks like for reference: https://i.stack.imgur.com/Qd7ll.png Here's a snippet of my ...

New messages are revealed as the chat box scrolls down

Whenever a user opens the chatbox or types a message, I want the scroll bar to automatically move down to show the most recent messages. I came across a solution that seems like it will do the trick: The issue is that despite implementing the provided cod ...

What is the best way to set up a server-sent-events broadcast feature in totaljs?

Let's imagine this situation: Client1 and Client2 are currently in session1 Meanwhile, Client3 and Client4 are part of session2 My aim now is to send event "a" to all clients in session1 exclusively. I came across this example: https://github ...

Exploring the functionality of CodePen's code editor in relation to developing a 2D shooting game

Recently, I created a straightforward 2D shooter game with all the code neatly organized in a single HTML file: (file_gist). When I tested the game in my chrome browser, everything worked flawlessly according to my intentions. However, upon transferring th ...

showing sections that collapse next to each other

I am currently designing a portfolio website using HTML, CSS, and vanilla JavaScript. I have implemented collapsing sections that expand when clicked on. However, the buttons for these sections are stacked vertically and I want to place them side by side. ...

Striving to implement a dynamic expand and collapse animation feature for a card in ReactJS

I'm attempting to create an expand and collapse animation effect on a card without relying on bootstrap or any external libraries. I have tried adding a transition property but it doesn't seem to work when the button is clicked. Here is the comp ...

What is the best way to pass the record ID of the row when the user clicks on the edit button?

The substance ID saved to the session is always the last record ID in the table. I need to send the record ID of the row when the user presses the edit button. Currently, every time a button is selected, the record ID sent is the last one in the table. I ...

Exploring methods for testing an HTML page that utilizes jQuery for DOM manipulations

Recently, I was tasked with creating an HTML page that utilized jQuery DOM manipulations. For instance, upon clicking the submit button, a success or error message should be displayed. Testing these functionalities is something I'm familiar with in An ...

Having trouble submitting data via a form during an AJAX call in Rails? experiencing issues with file uploads and receiving a 422 status code?

Having trouble uploading files with the bootsy gem? The issue may not be related to bootsy itself. When examining the form generated by bootsy, the following definition is present: <form class="bootsy-upload-form form-inline" id="new_image" data-type=" ...

JavaScript file encountering a problem with its global variables

What is causing the alert("2") to pop up first instead of it being second? Why am I encountering difficulty creating global variables c and ctx? Is there a way for me to successfully create these two global variables in order to utilize ...

What are some recommended approaches for implementing AJAX without relying on Rails' pre-built helpers?

Within my blog application, certain posts are displayed as excerpts, providing users with a preview of the initial 500 characters and an option to click through for the full post. The code snippet below illustrates this functionality: <% href = url_for ...

Reduce the count of products when the button is clicked

Hey there! I'm running a special promotion on my website with limited spots available at a reduced price. I'd love to have a real-time counter that shows the number of remaining spots, decreasing by one each time someone clicks the 'sign up& ...

Learning how to use Express.js to post and showcase comments in an HTML page with the help of Sqlite and Mustache templates

I am facing a persistent issue while trying to post new comments to the HTML in my forum app. Despite receiving various suggestions, I have been struggling to find a solution for quite some time now. Within the comments table, each comment includes attrib ...

Enhance the appearance of your Vuejs application with conditional style formatting

I want to enhance a basic table by applying conditional CSS styles to the cells based on their values. For example: If area1 == 'one', then assign the td-one CSS style. <tr v-for="version in versions" :key="version.id"> < ...

Arrange Drifting Components in a Fluid Layout Grid

I am in the process of creating a unique sticky note website where I want to arrange the notes in a grid format. As the window size decreases, the notes should automatically reorganize to fit the width and allow for vertical scrolling. My goal is to achiev ...