Mani
Mani

Reputation: 13

How to change the box color 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"
     "x"
  when "y"
     "y"
  when "z"
     "z"
end
end

in article.scss

.x {
  color: red;
}
.y{
  color: orange;
}
.z{
  color: green;
}

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

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

Upvotes: 0

Views: 244

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

If I parsed the question properly, you want to modify your css to:

#box .x { color: red }

and so on. The above will apply the color to the elements, having class="x" and id="box".

Or, you might change the ruby to:

<td id="box_<%= book_ty(article)%>"><%= article.book_type %></td>

and modify css to:

#box_x { color: red }

Or, combine the solutions above.

Upvotes: 0

Roman Kiselenko
Roman Kiselenko

Reputation: 44370

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?

Upvotes: 1

Related Questions