2017-07-09 41 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