如何 修改RAILS的默认错误提示 和 Rails validation error message 相关的合集
来源:互联网 发布:网络信息安全组制定 编辑:程序博客网 时间:2024/05/19 19:56
首先,有这样一个简单的问题,如果你想汉化,数字(不是长度)不能大于多少的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
- 如何 修改RAILS的默认错误提示 和 Rails validation error message 相关的合集
- 如何 修改RAILS的默认错误提示 和 Rails validation error message 相关的合集
- 关于Rails的错误提示 Rails flash error不消失
- 诗歌rails之 关于Rails的错误提示 Rails flash error
- rails中的正则表达式的相关错误提示
- Rails默认更新用户密码策略的修改
- Rails 3 修改url中默认的的/:id
- rails 迁移的相关知识
- Rails经常犯的错误二 - :include自定义的子集合
- Ruby on Rails 的检验方法(Validation Helpers)大全
- Ruby on Rails 的检验方法(Validation Helpers)大全
- Ruby on Rails 的检验方法(Validation Helpers)大全
- Ruby on Rails 的检验方法(Validation Helpers)大全
- Ruby on Rails 的检验方法(Validation Helpers)大全 (转)
- Rails 上面 Error Message 中文化
- rails中建立model 和 修改字段的方法
- rails中建立model 和 修改字段的方法
- rails中建立model 和 修改字段的方法
- ORB算法
- ABAP--如何生成SAP系统ABAP标准课程涉及到的数据表无数据的问题
- jquery validator addMethod 方法的使用
- fg、bg、jobs、&、ctrl + z等linux命令简介
- 最好用的 unity3d Json数据传输,插件JsonFx !!
- 如何 修改RAILS的默认错误提示 和 Rails validation error message 相关的合集
- 【转】Unity3D Platform Dependent Compilation
- jQuery validate表单正则验证功能详解
- ACM - 暑期第一天:求任意表达式的值
- ctrl+c,ctrl+d,ctrl+z在linux中意义
- 代理模式
- 诗歌rails之 关于Rails的错误提示 Rails flash error
- 解决source insight不识别.cc文件的问题
- codeforces 141E clearing up