簡體   English   中英

遍歷rails中的哈希數組,SystemStackError:堆棧級別太深錯誤

[英]Iterating through array of hashes in rails ,SystemStackError: stack level too deep error

我需要驗證包含哈希值的電話號碼。我正在使用phony_rails驗證電話號碼。 例如,

PHONE_NUMBER = [{ “類型”: “國際”, “號碼”: “655787”},{ “類型”: “國際”, “號碼”: “65578454547”}]。

我需要檢查該號碼是否是有效的電話號碼。 我嘗試下面的代碼。 我怎樣才能做到這一點?。 我做錯了什么?

validate :phone_number

  def phone_number
    phone_number.each {|phone| validates_plausible_phone phone[:number]}
  end

只需將您的驗證方法的名稱更改為其他名稱,因為您的類中已經具有方法phone_number 當您從phone_number方法內部調用phone_number ,將出現無限循環。

validate :valid_phone_number

def valid_phone_number
  phone_number.each {|phone| validates_plausible_phone phone[:number]}
end

另外,很明顯phone_number是一個數組,因此請將其重命名為phone_numbers (復數)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM