如何 修改RAILS的默认错误提示 和 Rails validation error message 相关的合集

来源:互联网 发布:网络信息安全组制定 编辑:程序博客网 时间:2024/05/19 19:56
首先,有这样一个简单的问题,如果你想汉化,数字(不是长度)不能大于多少的model错误提示信息,怎么做?
那么,先说验证是validates_numericality_of而数字大于多少的话,应该用:greater_than_or_equal_to
如果是希望了解,类似上面的信息的话,可以参考下面
Active Record Validations and Callbacks
而,我想说的是,如果你添加了

Ruby代码  收藏代码
  1. validates_numericality_of  
  2.   :greater_than_or_equal_to   

之后,当然就有错误提示了,可是你不想要系统的错误提示那么怎么办?
当然,最方便的办法是
Ruby代码  收藏代码
  1. :message=>“”  


确实,你写的错误提示显示了,可是问题来了,这样的话只要是这个字段验证不通过都会显示相同的错误提示。也就是说,
引用
:greater_than –
: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”的文件夹。

最后如下:

Ruby代码  收藏代码
  1. your_app/vendor/plugins/new_errors/lib  


3.在“new_errors”下建立一个init.rb“文件,内容为:

Ruby代码  收藏代码
  1. require "new_errors"  


4.在”lib“下新增一个叫”new_errors.rb“的文件。

内容如下:

Ruby代码  收藏代码
  1. module ActiveRecord  
  2.   
  3. class Errors  
  4.   
  5. @@default_error_messages = {  
  6.   
  7. :inclusion => "在列表中没有包含",  
  8.   
  9. :exclusion => "is reserved",  
  10.   
  11. :invalid => "is invalid",  
  12.   
  13. :confirmation => "doesn't match confirmation",  
  14.   
  15. :accepted => "must be accepted",  
  16.   
  17. :empty => "can't be empty",  
  18.   
  19. :blank => "can't be blank",  
  20.   
  21. :too_long => "is too long (maximum is %d characters)",  
  22.   
  23. :too_short => "is too short (minimum is %d characters)",  
  24.   
  25. :wrong_length => "is the wrong length (should be %d characters)",  
  26.   
  27. :taken => "已经存在了",  
  28.   
  29. :not_a_number => "is not a number。Numbers ars things like 12345 ok?"  
  30.   
  31. }  
  32.   
  33. end  
  34.   
  35. end  


在这里面你就可以修改错误提示信息了。

当重新启动WEBRick的时候,plugs将自动被装入。


问题只剩一个了,没有针对数字的错误提示:

实际上,是有的应该参考
Ruby代码  收藏代码
  1. ActiveRecord::Errors.default_error_messages  

Ruby代码  收藏代码
  1. Key Value  
  2. :inclusion  “is not included in the list”  
  3. :exclusion  “is reserved”  
  4. :invalid    “is invalid”  
  5. :confirmation   “doesn’t match confirmation”  
  6. :accepted   “must be accepted”  
  7. :empty  “can’t be empty”  
  8. :blank  “can’t be blank”  
  9. :too_long   “is too long (maximum is %d characters)”  
  10. :too_short  “is too short (maximum is %d characters)”  
  11. :wrong_length   “is the wrong length (should be %d characters)”  
  12. :taken  “has already been taken  
  13. :not_a_number   “is not a number  
  14. :greater_than   “must be greater than %d”  
  15. :greater_than_or_equal_to   “must be greater than or equal to %d”  
  16. :equal_to   “must be equal to %d”  
  17. :less_than  “must be less than %d”  
  18. :less_than_or_equal_to  “must be less than or equal to %d”  
  19. :odd    “must be odd”  
  20. :even   “must be even”  


update:

自己写的validate要写提示:
Ruby代码  收藏代码
  1. class Comment < ActiveRecord::Base  
  2.   validate :must_be_friends  
  3.   
  4.   def must_be_friends  
  5.     errors.add_to_base("Must be friends to leave a comment"unless commenter.friend_of?(commentee)  
  6.   end  
  7. end  
原创粉丝点击