你如何在Rails 3中使用number_to_phone?

我将电话号码存储在数据库中为“1234567890”。

你如何使用number_to_phone以“(123)456-7890”的forms很好地显示数字?

我尝试了以下但它只显示了数字。

谢谢。

您可以在应用程序助手中创建这样的简单帮助器

  def format_phone(phone, mobile=false) return phone if format.blank? groupings = format.scan(/d+/).map { |g| g.length } groupings = [3, 3, 4] unless groupings.length == 3 ActionController::Base.helpers.number_to_phone( phone, :area_code => format.index('(') ? true : false, :groupings => groupings, :delimiter => format.reverse.match(/[^d]/).to_s ) end 

然后在你看来做

 <%= format_phone(phone_number)%>