cycle(first_value, *values)
# Alternate CSS classes for even and odd numbers...
@items = [1,2,3,4]
<table>
<% @items.each do |item| %>
<tr class="<%= cycle("odd", "even") -%>">
<td>item</td>
</tr>
<% end %>
</table>
image_tag
生成image標簽
<%= image_tag(product.image_url, class: 'list_image') %>
會生成下面的代碼
<img alt="Leishen" class="list_image" src="/assets/leishen.jpg">
truncate
截取字符
#`strip_tags`用于去html標簽
truncate(strip_tags(product.description), length: 80)
truncate("Once upon a time in a world far far away")
# => "Once upon a time in a world..."
truncate("Once upon a time in a world far far away", length: 17)
# => "Once upon a ti..."
truncate("Once upon a time in a world far far away", length: 17, separator: ' ')
# => "Once upon a..."
truncate("And they found that many people were sleeping better.", length: 25, omission: '... (continued)')
# => "And they f... (continued)"
truncate("<p>Once upon a time in a world far far away</p>")
# => "<p>Once upon a time in a wo..."
truncate("Once upon a time in a world far far away") { link_to "Continue", "#" }
# => "Once upon a time in a wo...<a href="#">Continue</a>"
strip_tags(html)
清除html標簽
strip_tags("Strip <i>these</i> tags!")
# => Strip these tags!
strip_tags("<b>Bold</b> no more! <a href='more.html'>See more here</a>...")
# => Bold no more! See more here...
strip_tags("<div id='top-bar'>Welcome to my website!</div>")
# => Welcome to my website!
sprintf
格式化字符串
sprintf("$%0.02f", 12)
自動轉(zhuǎn)換成$12.00
時間轉(zhuǎn)換方法
product.updated_at.localtime.strftime('%Y/%m/%d %H:%S')
一般我們通過多對多關(guān)系獲取到的數(shù)據(jù)都是一個集合對象鸠补,我們需要通過使用to_a
方法將其轉(zhuǎn)換成數(shù)組
line_items.to_a.sum {|item| item.total_price}