一个用于处理日期,数字和文本格式的“帮助方法”。
<%= distance_of_time_in_words(Time.now, Time.local(2005, 12, 25)) %>
248 days
<%= distance_of_time_in_words(Time.now, Time.now + 33, false) %>
1 minute
<%= distance_of_time_in_words(Time.now, Time.now + 33, true) %>
half a minute
<%= time_ago_in_words(Time.local(2004, 12, 25)) %>
116 days
<%= human_size(123_456) %>
120.6 KB
<%= number_to_currency(123.45) %>
$123.45
<%= number_to_currency(234.56, :unit => "CAN$", :precision => 0) %>
CAN$235.
<%= number_to_percentage(66.66666) %>
66.667%
<%= number_to_percentage(66.66666, :precision => 1) %>
66.7%
<%= number_to_phone(2125551212) %>
212-555-1212
<%= number_to_phone(2125551212, :area_code => true, :delimiter => " ") %>
(212) 555 1212
<%= number_with_delimiter(12345678) %>
12,345,678
<%= number_with_delimiter(12345678, delimiter = "_") %>
12_345_678
<%= number_with_precision(50.0/3) %>
16.667
<%= number_with_precision(50.0/3, 1) %>
16.7
debug()方法使用YAML转储它的参数,并转义结果以便于它能够被显示在一个HTML页内。这对想查看“模型”对象内变量或请求的参数时,很帮助。
<%= debug(params) %>
--- !ruby/hash:HashWithIndifferentAccess
name: Dave
language: Ruby
action: objects
controller: test
还有处理文本的另一个帮助方法。这些方法可截取字符串,以及高亮度显示字符串内单词(useful to show search results, perhaps)。
<%= simple_format(@trees) %> 格式化一个字符串,保留原有的行和段落的划分。You could give it the plain text of the Joyce Kilmer poem Trees and it would add the HTML to format it as follows:
<p> I think that I shall never see
<br />A poem lovely as a tree.</p>
<p>A tree whose hungry mouth is prest
<br />Against the sweet earth’s flowing breast;
</p>
<%= excerpt(@trees, "lovely", 8) %>
...A poem lovely as a tre...
<%= highlight(@trees, "tree") %>
I think that I shall never see
A poem lovely as a <strong class="highlight">tree</strong>.
A <strong class="highlight">tree</strong> whose hungry mouth is prest
Against the sweet earth’s flowing breast;
<%= truncate(@trees, 20) %>
I think that I sh...
There’s a method to pluralize nouns.
<%= pluralize(1, "person") %> but <%= pluralize(2, "person") %>
1 person but 2 people