正在看railsspace_building_a_social_networking_website_with_ruby_on_rails一书,Rails_Space的测试代码,在测试时发现用下列验证输入信息过长的错误。在Rails2.2.2中发生错误。
error_messages = ActiveRecord::Errors.default_error_messages
sprintf(error_messages[:too_long], length)
经查看代码,在rails-2.0前,可以把default_error_messages中%d替换成数字。
在Rails2.2.2中,default_error_messages定义发生了变化,不能把{{counter}}提换成数字。可以在script/console 中,通过输入ActiveRecord::Errors.default_error_messages查看详细内容。
2.0以前,在validations.rb中
@@default_error_messages = {
:inclusion => "is not included in the list",
:exclusion => "is reserved",
:invalid => "is invalid",
:confirmation => "doesn't match confirmation",
:accepted => "must be accepted",
:empty => "can't be empty",
:blank => "can't be blank",
:too_long => "is too long (maximum is %d characters)",
:too_short => "is too short (minimum is %d characters)",
:wrong_length => "is the wrong length (should be %d characters)",
:taken => "has already been taken",
:not_a_number => "is not a number",
:greater_than => "must be greater than %d",
:greater_than_or_equal_to => "must be greater than or equal to %d",
:equal_to => "must be equal to %d",
:less_than => "must be less than %d",
:less_than_or_equal_to => "must be less than or equal to %d",
:odd => "must be odd",
:even => "must be even"
}
2.2.2,在validations.rb中
def default_error_messages
ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages
has been deprecated. Please use
I18n.translate('activerecord.errors.messages').")
I18n.translate 'activerecord.errors.messages'
end
error_messages = ActiveRecord::Errors.default_error_messages
sprintf(error_messages[:too_long], length)
经查看代码,在rails-2.0前,可以把default_error_messages中%d替换成数字。
在Rails2.2.2中,default_error_messages定义发生了变化,不能把{{counter}}提换成数字。可以在script/console 中,通过输入ActiveRecord::Errors.default_error_messages查看详细内容。
2.0以前,在validations.rb中
@@default_error_messages = {
:inclusion => "is not included in the list",
:exclusion => "is reserved",
:invalid => "is invalid",
:confirmation => "doesn't match confirmation",
:accepted => "must be accepted",
:empty => "can't be empty",
:blank => "can't be blank",
:too_long => "is too long (maximum is %d characters)",
:too_short => "is too short (minimum is %d characters)",
:wrong_length => "is the wrong length (should be %d characters)",
:taken => "has already been taken",
:not_a_number => "is not a number",
:greater_than => "must be greater than %d",
:greater_than_or_equal_to => "must be greater than or equal to %d",
:equal_to => "must be equal to %d",
:less_than => "must be less than %d",
:less_than_or_equal_to => "must be less than or equal to %d",
:odd => "must be odd",
:even => "must be even"
}
2.2.2,在validations.rb中
def default_error_messages
ActiveSupport::Deprecation.warn("ActiveRecord::Errors.default_error_messages
has been deprecated. Please use
I18n.translate('activerecord.errors.messages').")
I18n.translate 'activerecord.errors.messages'
end