Rails 常用帮助函数

cycle(first_value, *values)
# Alternate CSS classes for even and odd numbers...
@items = [1,2,3,4]


<% @items.each do |item| %>
">


<% end %>
item

image_tag生成image标签
<%= image_tag(product.image_url, class: 'list_image') %>
会生成下面的代码
Leishen

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("

Once upon a time in a world far far away

") # => "

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...Continue"

strip_tags(html)清除html标签

strip_tags("Strip these tags!")
# => Strip these tags!

strip_tags("Bold no more!  See more here...")
# => Bold no more!  See more here...

strip_tags("
Welcome to my website!
") # => Welcome to my website!

sprintf格式化字符串
sprintf("$%0.02f", 12)自动转换成$12.00

时间转换方法
product.updated_at.localtime.strftime('%Y/%m/%d %H:%S')

一般我们通过多对多关系获取到的数据都是一个集合对象,我们需要通过使用to_a方法将其转换成数组
line_items.to_a.sum {|item| item.total_price}

你可能感兴趣的:(Rails 常用帮助函数)