首先,有这样一个简单的问题,如果你想汉化,数字(不是长度)不能大于多少的model错误提示信息,怎么做?
那么,先说验证是validates_numericality_of而数字大于多少的话,应该用:greater_than_or_equal_to
如果是希望了解,类似上面的信息的话,可以参考下面
Active Record Validations and Callbacks
而,我想说的是,如果你添加了
之后,当然就有错误提示了,可是你不想要系统的错误提示那么怎么办?
当然,最方便的办法是
确实,你写的错误提示显示了,可是问题来了,这样的话只要是这个字段验证不通过都会显示相同的错误提示。也就是说,
那么,我们可能用到下面的东西:
通过插件汉化Rails错误提示信息
1.在 your_app/vendor/plugins下建立一个新的文件夹“new_errors”
2.在新建的文件夹“new_errors”下建一个“lib”的文件夹。
最后如下:
3.在“new_errors”下建立一个init.rb“文件,内容为:
4.在”lib“下新增一个叫”new_errors.rb“的文件。
内容如下:
在这里面你就可以修改错误提示信息了。
当重新启动WEBRick的时候,plugs将自动被装入。
问题只剩一个了,没有针对数字的错误提示:
实际上,是有的应该参考
update:
自己写的validate要写提示:
那么,先说验证是validates_numericality_of而数字大于多少的话,应该用:greater_than_or_equal_to
如果是希望了解,类似上面的信息的话,可以参考下面
Active Record Validations and Callbacks
而,我想说的是,如果你添加了
- validates_numericality_of
- :greater_than_or_equal_to
之后,当然就有错误提示了,可是你不想要系统的错误提示那么怎么办?
当然,最方便的办法是
- :message=>“”
确实,你写的错误提示显示了,可是问题来了,这样的话只要是这个字段验证不通过都会显示相同的错误提示。也就是说,
引用
:greater_than –
:greater_than_or_equal_to –
:equal_to –
:less_than –
:less_than_or_equal_to –
:odd –
:even –
都会显示相同的错误提示。
:greater_than_or_equal_to –
:equal_to –
:less_than –
:less_than_or_equal_to –
:odd –
:even –
那么,我们可能用到下面的东西:
通过插件汉化Rails错误提示信息
1.在 your_app/vendor/plugins下建立一个新的文件夹“new_errors”
2.在新建的文件夹“new_errors”下建一个“lib”的文件夹。
最后如下:
- your_app/vendor/plugins/new_errors/lib
3.在“new_errors”下建立一个init.rb“文件,内容为:
- require "new_errors"
4.在”lib“下新增一个叫”new_errors.rb“的文件。
内容如下:
- module ActiveRecord
- class Errors
- @@default_error_messages = {
- :inclusion => "在列表中没有包含",
- :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 => "已经存在了",
- :not_a_number => "is not a number。Numbers ars things like 12345 ok?"
- }
- end
- end
在这里面你就可以修改错误提示信息了。
当重新启动WEBRick的时候,plugs将自动被装入。
问题只剩一个了,没有针对数字的错误提示:
实际上,是有的应该参考
- ActiveRecord::Errors.default_error_messages
- Key Value
- :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 (maximum 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”
update:
自己写的validate要写提示:
- class Comment < ActiveRecord::Base
- validate :must_be_friends
- def must_be_friends
- errors.add_to_base("Must be friends to leave a comment") unless commenter.friend_of?(commentee)
- end
- end