我不明白为什么以下不在Rails 3中工作。我得到“未定义的局部变量或方法`custom_message'”错误。Rails 3:验证中的自定义错误消息
validates :to_email, :email_format => { :message => custom_message }
def custom_message
self.to_name + "'s email is not valid"
end
我也尝试使用:消息=>:自定义信息,而不是如在rails-validation-message-error交没有运气建议。
:email_format是位于lib文件夹一个自定义的验证:
class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
object.errors[attribute] << (options[:message] || 'is not valid')
end
end
end
我可以使用您的确切代码复制您的错误,但是当我按照您的建议将其更改为':message =>:custom_message'时,错误消失。 – Dusty 2010-09-25 19:46:26