1

I want to change the aspect of some html elements representing table rows from a model. In particular, my Component model has a :rating column:

create_table "components", force: true do |t|
  t.string   "name"
  t.string   "description"
  t.string   "repository_url"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "rating", default: 5 # color in the view to be defined by this value
end

And, in the view, the existing components (Component.all) are rendered as links:

- @components.each do |components|
 = link_to component.name, component, class: "rating_#{component.rating}"

As you can imagine, the way I am handling this by defines the style for rating_1...rating_10 in the corresponding css file:

.componenttag.rating_1 {
    background-color : black
}

...

.componenttag.rating_10 {
    background-color : green
}

This works as a simple solution, but I have the impression that there is a better (and more elegant) way of solving this problem or other similar ones. Am I wrong? :)

Thanks in advance.

Яois
  • 3,838
  • 4
  • 28
  • 50
  • 1
    This seems quite elegant to me. I wouldn't do it any other way. – Ruby Racer Dec 22 '14 at 21:49
  • Thanks @RubyRacer . The truth is I have little experience with Rails and webapps in general, and repeating the `.rating_N` part in the CSS looked to me as a newbie thing :D – Яois Dec 23 '14 at 09:15

0 Answers0