2
我的理解是,我可以在本地化文件中自定義錯誤消息。我目前正試圖以自定義一個無效的用戶名字段中顯示錯誤消息對我的用戶模型:Rails自定義驗證錯誤消息不起作用
User.rb
class User < ActiveRecord::Base
validates :username, length: { within: 3..25 },
format: /\A(?=.*[a-z])[a-z\d]+\Z/i,
uniqueness: true
en.yml(本地化文件)
en:
activerecord:
errors:
models:
user:
attributes:
username:
format: "CUSTOM MESSAGE!"
但是,我收到的錯誤消息是默認的「無效」消息(或者如果我獲得full_messages
版本,「用戶名無效」)。