本文转载于SegmentFault社区作者:shiweifu在 Rails 中,Helper 是指什么?Helper 是一个函数(多数时候),用于 Rails 视图之间,共享可复用的代码。Rails 内置一组 Helper 函数。其中一个是 time_ago_in_words。例子time_ago_in_words(Time.now)# "less than a minute"time_ago_in_words(Time.now + 60)# "1 minute"time_ago_in_words(Time.now + 600)# "10 minutes"这个函数,用于显示指定格式的日期。另一个用于 Rails 视图的 Helper 函数是 number_to_human。例子:number_to_human(10_000)# "10 Thousand"当你想要将数字显示为可读的形式时,这个函数很有用。你可以在 Rails 文档中,找到更多的内容:https://api.rubyonrails.org/那么,该如何定义自己的 Helper 函数呢?编写自己的 Helper 函数如果你要编写 Helper 函数,正确的路径是 app/helpers,然后将你的 H
………………………………