-2

I'm having trouble display specific range of data in do-loop in Ruby. I Have this example code:

Post

<!-- Display loop only 9 sections -->
<div class="row">
<% @post.each do |post| %>
    <div clas="blog">
        <h3 class="heading"><%= post.title %></h3>
    </div>
</div>
<!-- END => Display loop only 9 sections -->

<div class="row">
    <div clas="banner">
        <img src="images/banner.jpg" alt="banner">
    </div>
</div>

<!-- Continue display loop for 10 and until latest sections -->
<div class="row">
<% @post.each do |post| %>
    <div clas="blog">
        <h3 class="heading"><%= post.title %></h3>
    </div>
</div>
<!-- END => -->

In this situation I want to add a banner section after it displays 9 posts:

<div class="row">
    <% @post.each do |post| %>
        <div clas="blog">
            <h3 class="heading"><%= post.title %></h3>
        </div>
    </div>
    <!-- END => Display loop only 9 sections -->

<!-- I will add this banner section after posts displays 9 posts -->
<div class="row">
        <div clas="banner">
            <img src="images/banner.jpg" alt="banner">
        </div>
    </div>

and then I want to continue the post display from post 10 up to latest posts after I added the banner section:

<!-- Continue display loop for 10 and until latest sections -->
<div class="row">
<% @post.each do |post| %>
    <div clas="blog">
        <h3 class="heading"><%= post.title %></h3>
    </div>
</div>
<!-- END => -->

In my situation, the two post sections will display all the data in post. How do you limit 9 posts in the 1st div element, and continue the latest posts in the 3rd div element below the banner section using the do loop? I want to continue the latest posts in my 3rd section.

Is there any easy way to do this?

I'm currently new to rails app and I'm still exploring the syntax of the program.

laurence keith albano
  • 1,409
  • 4
  • 27
  • 59
  • What do you want to happen if there are fewer than 9 posts? Should the banner still appear at the bottom? – Tom Lord Jul 06 '20 at 12:07
  • In my DB, I have 50 posts.. I want to limit 9 posts only on first div element after that I will add another section that displays the banner, on the 3rd div element, I want to continue the do loop that displays 10-50 posts or 10 to latest posts. That's basically I want to achieve @Tom Lord. – laurence keith albano Jul 06 '20 at 12:10
  • 1
    I understand the design, I was asking because you didn't make it clear that there are guaranteed to always be more than 10 posts. Usually, page designs do need to consider "what if there are more/less items?". – Tom Lord Jul 06 '20 at 12:19

3 Answers3

2

You can use .each_with_index method to determine if this exact element of the collection is 9th, here is the documentation link: https://ruby-doc.org/core-2.7.1/Enumerable.html#method-i-each_with_index

<!-- Display loop only 9 sections -->
<div class="row">
<% @post.each_with_index do |post, index| %>
    <div clas="blog">
        <h3 class="heading"><%= post.title %></h3>
    </div>
</div>

  <% if index == 8 %>
    <div class="row">
      <div clas="banner">
        <img src="images/banner.jpg" alt="banner">
      </div>
    </div>
  <% end %>
<% end %>

Something like this should work

1

For the first 9 posts:

<% @post.limit(9).each do |post| %>

And the other posts below the banner:

<% @post.drop(9).each do |post| %>
Gustavo Gabriel
  • 1,378
  • 2
  • 17
  • 28
0

Maybe:

<% @post[0..8].each do |post| %>
...
<% @post[9..-1].each do |post| %>

or:

BANNER_POSITION = 9
<% @post[0...BANNER_POSITION].each do |post| %>
...
<% @post[BANNER_POSITION..-1].each do |post| %>
Igor
  • 1,253
  • 1
  • 25
  • 34