开发者

Is it possible to use a class method to define a CSS style?

开发者 https://www.devze.com 2023-01-21 10:48 出处:网络
I am trying to build in color-coding in my site for a particular class method, such that some values display in a different color if they are low.

I am trying to build in color-coding in my site for a particular class method, such that some values display in a different color if they are low.

I just defined a class method that translates numbers stored in my database to words that are 开发者_如何学JAVAdisplayed to my users.

# model.rb
def numbers_explained
  numbers_explained = case number
    when 0 then "Low"
    when 1 then "OK"
    when 2 then "OK"
    when 3 then "High"
  end
end 

The other thing I'd like to do is always display "Low" in red.

Can we 'scope' CSS styles like we do with data? Could I attach something like color:red !important;?


I don't think there's any fancy-easy way to do this with Rails. This is pure presentation logic, so I'd define a helper method in model_helper.rb to wrap it in a span with a class:

def numbers_explained(model)
  content_tag_for(:span, model, :class => (model.number ? '' : 'low')) do
    model.numbers_explained
  end
end

And add CSS for class low to display it in red.

0

精彩评论

暂无评论...
验证码 换一张
取 消