簡體   English   中英

Rails驗證錯誤消息,自定義屬性名稱

[英]Rails validation error messages, customizing the attribute name

例如,如果模型具有名為“unit”的屬性,但在您的視圖中,您將此屬性稱為“單價”,但是當您進行驗證時,錯誤消息默認為“單位”,如何將其修改為“單價”?

使用本地化設置屬性的“英語”名稱。 您可以設置單數和復數名稱:

en:
  activerecord:
    attributes:
      product:
        unit:
          one:   Unit price
          other: Unit prices

我不確定如何更改列名稱,但以下是一個有效的解決方法

在您的模型中創建一個名為unit_price的虛擬屬性

這樣的事情

attr_accessor :unit_price

validates_presence_of :unit_price, :message => "This is a custom validation message"

def before_validation
   self.unit_price = self.unit
end

干杯

sameera

暫無
暫無

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

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