2017-07-09 315 views
0

我在我的应用程序下面的行,Ruby on Rails的number_to_phone helper方法

<%= f.telephone_field number_to_phone(1235551234) , placeHolder: 'Telephone', :class => 'form-control' %> 

但是,当我浏览的页面,我一直得到以下错误。

NoMethodError - undefined method `123-555-1234' for #<User:0x007fcca00ef690>: 

我试过那个,但它也没有工作。

<%= f.telephone_field ActionView::Helpers::NumberHelper.number_to_phone(1235551234) , placeHolder: 'Telephone', :class => 'form-control' %> 

我看不到我在想什么?

任何建议,

谢谢。

回答

0

NoMethodError - 未定义的方法`123-555-1234' 的用户 :0x007fcca00ef690

telephone_field(对象,方法,选项= {})公共

返回类型‘电话’的text_field 。

telephone_field( “用户”, “手机”)

#=><input id="user_phone" name="user[phone]" type="tel" />

问题是number_to_phone帮手。所有形式佣工期待method(模型的属性)作为秒参数。您应通过要保存输入的User型号的属性。例如让我们说你的属性名称为phone_number,然后根据你的建议写出来像下面

<%= f.telephone_field :phone_number , placeHolder: 'Telephone', :class => 'form-control' %> 

而且在你显示phone_number认为,使用number_to_phone帮手

<%= number_to_phone(@user.phone_number) %> 
+0

,我已签代码与<%= f.telephone_field number_to_phone(@ user.tel),placeHolder:'电话',:class =>'form-control'%>,但我得到同样的错误:( –

+0

@HilmiYamcı你没有仔细阅读我的答案,再读一遍! – Pavan

+1

@HilmiYamcı帕文说的是,你**不能在c时使用助手重复'input' **,所以不要在't.telephone_field'中使用助手(如答案所示)。您只能使用助手来显示'phone_number'字段的实际值。 – Gerry