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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:38:52+00:00 2026-06-09T03:38:52+00:00

I have an array of things, things = [1, 500, 900, 0, -105, -8,

  • 0

I have an array of things, things = [1, 500, 900, 0, -105, -8, 16, 4] and I want to display them in red, green or black in my view based on whether they’re larger than zero, smaller than zero or equal to zero.

Right now my code looks like:

<%= @things.each do |p|%>
 <% if p > 0 %>
      <%= p  %>
    <% elsif p < 0 %>
      <%= p %>
    <% else %>
      <%= p %>
 <% end %>
<% end %>

I’m not displaying the numbers correctly to start, and I’ve gone through a few iterations of putting them in divs and nothing seems to work. Once I get ’em in classes, I can add colors pretty easily. (I also need to keep them in the same order they’re in in the array)

Any help would be greatly appreciated.

  • 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-09T03:38:53+00:00Added an answer on June 9, 2026 at 3:38 am

    Not sure what’s the issue here

     <% @things.each do |p|
         if p < 0
           class_name = 'red'
         elsif p > 0 
           class_name = 'green'
         else 
           class_name = 'black'
         end %>
         <div class="<%=class_name%>"><%= p %></div> 
    <% end %>
    

    Then css :

    .red{color:red;}
    .green{color:green;}
    .black{color:black;}
    

    Edit : In your example, you were using <%= @things.each. Don’t use <%= for things that are not supposed to output anything

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of various things including scalars and arrays. I want to
I have an Array of number values, and I want to randomly select a
I have an array of empty cells and ones that I want to convert
I have so many cells in table view say nearly 500. Each and every
So I have some array called params[]; there are things like a[0] = 2
I have an array with more than 134675+ values, I need to insert them
I have an array in c# that is 1-based (generated from a call to
Hi I have an array of words that I want to do a couple
I have javascript code some thing like this -- var count=3; var pl=new Array(count);
I have array INPUTFILES with n files INPUTFILES=( file_0 ... files_n-1 ) And i

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.