Html Rails或CSS替换表格单元格内容(如果是特定字符串)?
我有以下HTML表格代码:Html Rails或CSS替换表格单元格内容(如果是特定字符串)?,html,css,ruby-on-rails,if-statement,html-table,Html,Css,Ruby On Rails,If Statement,Html Table,我有以下HTML表格代码: <table> <tr> <th>Time</th> <th>Source</th> <th class="hide-on-phones">Destination</th> <th>Duration</th> </tr> <tr> <td>11 Mar. 2012
<table>
<tr>
<th>Time</th>
<th>Source</th>
<th class="hide-on-phones">Destination</th>
<th>Duration</th>
</tr>
<tr>
<td>11 Mar. 2012 - 16:37</td>
<td>0778593789</td>
<td class="hide-on-phones">08456783850</td>
<td>00:03:10</td>
</tr>
</table>
为什么不创建一个帮助器方法来将目标转换为要显示的值? 我假设您有一组不同的不会更改的目的地,这将允许您在应用程序中硬编码它们,而无需将它们存储在DB中,以便稍后通过应用程序进行修改 像这样的
def format_destination(destination_number)
case destination_number
when 1234
image_tag "number_1234.jpg"
when 5678
image_tag "number_5678.jpg"
else
image_tag "default_number.jpg"
end
end
我想了想,但还是弄糊涂了!然后我会在我的视图中调用吗?在你的视图中,你只需要使用
,尽管如果它与你正在使用的图像紧密相连,为什么不首先将图像标签放在自己的视图中呢?如果我能帮得上忙,它不会被图像替换,我想用特定的公司名称替换特定的数字。下面的方法行吗?是的,假设您有一个名为call
的对象,该对象的方法destination
提供了要传递到格式\u destination
帮助程序的值。08456742850
是一个格式错误的八进制数。destination\u number
实际上是一个数字还是一个字符串?如果是一个字符串(我假设),那么在中也需要字符串。如果大于四个或五个条目,您可能希望使用散列而不是大小写开关。它只是简单的代码。目的地编号是一个字符串,您可以向我介绍示例代码或教程?当您尝试匹配字符串时,请在“08456742850”时使用
。在入门教程中,这一级别的Ruby可能是最好的;恐怕我现在还不了解好的方面。pragprog.com有很好的材料,但afaicr不是免费的。
def format_destination(destination_number)
case destination_number
when 1234
image_tag "number_1234.jpg"
when 5678
image_tag "number_5678.jpg"
else
image_tag "default_number.jpg"
end
end