helpers中的帮助方法

一个用于处理日期,数字和文本格式的“帮助方法”。

<%= 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

 

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值