How to change the color of the box based on the class value


I have a code in a loop like this

<td id = box, class =<%= book_ty(article)%>><%= article.book_type %></td>

When the book_ty equal to x the box will become red color, if it is y the box will be orange color. Now the colors are directly applied to article.book_type I want apply that colors to id

I define a method in articles.helper.rb i.e

def book_ty(article)
 case article.book_type
  when "x"
  when "y"
  when "z"

in article.scss

.x {
  color: red;
  color: orange;
  color: green;

#box {
 width: 50px;
 height: 50px;
 display: inline;
 margin-right: 10px;

How to change the box color based on the class value?

You don't need a helper method for it, article.book_type already return the class for td.

<td id="box" class="<%= article.book_type %>"><%= article.book_type %></td>

But your title still unclear for me

How to get css id based on class value?