📜  验证自定义列名称上的 rails 模型 - Ruby 代码示例

📅  最后修改于: 2022-03-11 15:04:47.050000             🧑  作者: Mango

代码示例1
# model
validates   :email,    :uniqueness => { message: "is wrong" }
validates   :name,    :uniqueness => { message: "Your name is wrong" }

HUMANIZED_ATTRIBUTES = {
  :email => "E-mail address",
  :name => "" # don't include column name in error
}

def self.human_attribute_name(attr, options={})
  HUMANIZED_ATTRIBUTES[attr.to_sym] || super
end

# now the errors will be:
"E-mail address is wrong"
"Your name is wrong"

# instead of:
"Email E-mail address is wrong"
"Name Your name is wrong"