验证后未显示消息效果

时间:2012-10-03 04:18:59

标签: ruby-on-rails

我是Rails的新手...经过makin验证和消息传递...仍然无法看到效果......没有反映消息......我的代码是

class Users < ActiveRecord::Base
  attr_accessible :account_expired, :account_locked, :enabled, :id, :password, :password_expired, :username, :version

  validates :username,:password, :presence =>{:message => "Must be not blank"}

end

3 个答案:

答案 0 :(得分:0)

如果我理解你是正确的 - 你需要custom validators并添加如下错误信息:

errors.add(:base,  'Must be not blank')

您看到为字符串添加用户名的原因是因为rails调用

errors.add(:attribute, your_message) 

引擎盖下

答案 1 :(得分:0)

如果您想打印错误,请尝试:

model.errors.full_messages.each do |message|
  puts message

答案 2 :(得分:0)

您也可以传递自定义验证

例如

# in your model

validate:username_blank

def username_blank
 if self.username.blank? || self.username == ""
   errors[:base] << "Username Can't be Blank" # or whatever your message
 end
end