Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8437115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:20:50+00:00 2026-06-10T07:20:50+00:00

I have a loop for my Activity model <% @activities.each do |activity| %> <div>

  • 0

I have a loop for my Activity model

<% @activities.each do |activity| %>
<div>
    <div class="cell">
        <%= image_tag(activity.image) %>
    </div>
    <div class="cell">
        <h4><%= activity.title %></h4>
        <p><%= activity.description %></p>
        <span><%= activity.distance %></span>
    </div>
</div>
<% end %>  

I want to create a zig-zag effect, so I need to re-arrange the HTML for every even activity, so the markup will look like this:

<div>
  <div class="cell">
    <h4><%= activity.title %></h4>
    <p><%= activity.description %></p>
    <span><%= activity.distance %></span>
  </div>
  <div class="cell">
    <%= image_tag(activity.image) %>
  </div>
</div>

Is there a way to do this with an if statement? Something like this:

<% if activity.odd? %>
 <% @activities.each do |activity| %>
   <div>
     <div class="cell">
   <%= image_tag(activity.image) %>
 </div>
 <div class="cell">
   <h4><%= activity.title %></h4>
   <p><%= activity.description %></p>
   <span><%= activity.distance %></span>
 </div>
   </div>
  <% end %>
<% else %>
  <% @activities.each do |activity| %>
    <div>
      <div class="cell">
    <h4><%= activity.title %></h4>
    <p><%= activity.description %></p>
    <span><%= activity.distance %></span>
  </div>
  <div class="cell">
    <%= image_tag(activity.image) %>
      </div>
</div>
<% end %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T07:20:52+00:00Added an answer on June 10, 2026 at 7:20 am

    There are a few ways to do this.

    1. The first and best way is to do this with CSS’s nth-child selector, assuming it’s possible to achieve your desired effect purely through CSS. Use nth-child(odd) or nth-child(even), as described in Alternate table row color using CSS?

    2. If you’re iterating over a collection using .each, you can add a .with_index and check whether the index is odd or even:

      <% @activities.each.with_index do |activity, index| %>
        <% if index.odd? %>
          ...
        <% else %>
          ...
        <% end %>
      <% end %>
      

      Note that by default the index is zero-based, so your first element will be considered “even”. You can pass a starting index of 1 to with_index if you want the alternating to start odd instead:

      <% @activities.each.with_index(1) do |activity, index| %>
      
    3. An alternative to using with_index is to use Rails’ cycle helper, which “magically” returns the next argument in rotation each time its invoked:

      <% @activities.each do |activity| %>
        <% if cycle('odd', 'even') == 'odd' %>
          ...
        <% end %>
      <% end %>
      

    Also note that this is a fantastic opportunity for refactoring. You should distill the two reused cell divs into their own partials and render them in the order you want:

    <% if index.odd? %>
      <%= render h4_part %>
      <%= render img_part %>
    <% else %>
      <% render img_part %>
      <% render h4_part %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have loop and each iteration of Hash looks like this: [1, {:clid=>1, :nvz=>4,
I have a loop that enters each non-empty text field into a database: foreach
I have a loop $(.box).find('.video, .print, .web').closest('.box').each(function (index) { $(this).delay(100 * index).fadeIn(300); }); This
I need to have a class with one activity that is performed once per
I have following code: public class readSensorsData extends Activity implements SensorListener { /** Called
I have button of pick image from android gallery in one activity name DrawingActivity.
I have main activity which call other object, for example GPS class. This GPS
I have the following class: public class MainActivity extends Activity { /** Called when
i have executed the following code public class Activity_Threads_Handler extends Activity { private int
I have an Activity with three images. When one image is clicked, all the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.