I have a table in Rails where a 开发者_运维知识库number of columns are full of true/false values.
How can I do a performant substitution, replacing a table full of true
s and false
s with small images to represent true and false?
I would usually add a class to a <span>
or <div>
element, and then use CSS selectors to apply the appropriate background image for each case.
In the view...
<span class='foo-indicator <%= @item.foo? ? 'foo' : 'not-foo' %>'> </span>
In the CSS stylesheet...
.foo-indicator {
/* Specify height, width, positioning, etc. */
}
.foo {
background-image: url('../images/is-foo.png')
}
.not-foo {
background-image: url('../images/not-foo.png')
}
Have a helper method, something like this, in your application helper
def display_status(status)
(status == true) ? image("true.png") : image("false.png")
end
private
def image(name)
"/images/#{name}"
end
And when you are creating the table, call the 'display_status' method with the parameter.
the same idea as Steve Jorgensen but without css
<img src="<%= @item.foo? ? "/images/true.png" : "/images/false.png" %>"></img>
精彩评论