开发者

Replacing "true"/"false" with images

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 trues and falses 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' %>'>&nbsp;</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>

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜