Rails

Cycle Background Colors Of Elements In Rails

Posted by Weston Ganger on July 04, 2014

I was previously having to add extra lines of HTML to achieve this. Stumbled upon this is the rails docs, gonna really come in handy.


<% @posts.each do %>
  <div style="background-color: <%= cycle("red","green","blue", name: "colors");" %></div>
<% end %>

If you need to use that cycle color or value more than once in the loop it looks like this


<% @posts.each do %>
  <div style="background-color: <%= cycle("red","green","blue", name: "colors");" %>
    <span style="background-color: <%= current_cycle %>">sample text</span>
  </div>
<% end %>

Posted in Rails and Tagged with rails 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts