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 7569577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:12:03+00:00 2026-05-30T15:12:03+00:00

I am working within a Ruby on Rails app. I have a loop that

  • 0

I am working within a Ruby on Rails app. I have a loop that creates divs like this:

<% @snorks.each do |snork| -%>
  <div>
    <%= snork %>
  </div>
<% end %>

And I need the output to have every other div be floated left or right like this:

<div class="left">
  Allstar Seaworthy
</div>
<div class="right">
  Casey Kelp
</div>
<div class="left">
  Dimmy Finster
</div>
<div class="right">
  Daffney Gillfin
</div>
<div class="left">
  Tooter Shellby
</div>
<div class="right">
  Dr. / Uncle Galeo
</div>

Additionally, I need to add a div with class="clear" every two divs, like this:

<div class="left">
  Allstar Seaworthy
</div>
<div class="right">
  Casey Kelp
</div>
<div class="clear"></div>
<div class="left">
  Dimmy Finster
</div>
<div class="right">
  Daffney Gillfin
</div>
<div class="clear"></div>
<div class="left">
  Tooter Shellby
</div>
<div class="right">
  Dr. / Uncle Galeo
</div>
<div class="clear"></div>

I have researched, and found a few posts saying that the alternate classes can be accomplished easily by using cycle(), and that does work. However, when I use it in two places within the loop it stops working right and just outputs something like this:

<div class="left">
  Allstar Seaworthy
</div>
<div class="left">
  Casey Kelp
</div>
<div class="left">
  Dimmy Finster
</div>
<div class="left">
  Daffney Gillfin
</div>
<div class="left">
  Tooter Shellby
</div>
<div class="left">
  Dr. / Uncle Galeo
</div>

What’s the best practices way in Ruby on Rails to alternate classes in a loop, and also add something every other loop?

  • 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-05-30T15:12:05+00:00Added an answer on May 30, 2026 at 3:12 pm

    According to the documentation, if you need nested ones you name them. Otherwise they will both share the name “default” and conflict.

    <% @snorks.each do |snork| -%>
      <div class="<%= cycle('left', 'right') -%>">
        <%= snork %>
      </div>
      <%= cycle('','<div  class="clear"></div>', :name=>"cleardiv") %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fully working Setup project within Visual Studio 2008 that takes inputs
I am working within a site framework that I cannot modify. This framework adds
I am working on a Ruby on Rails 3(.0) application that uses a Rails
First I'm quite new to ruby and rails... I have a little application working
I am working within an intranet environment. We have both a production and development
It seems to me that Response.Cache.SetVaryByCustom(mykey); is not working from within ASCX User Controls.
I'd like to change the pwd of the current shell from within a ruby
I currently have a Rails 3.0.10 app and I made two models with simple
I'm working with Ruby on Rails 3, Cucumber, and Capybara I've been searching for
I am working within a enterprise architecture that is processing a large amount of

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.