如何 修改RAILS的默认错误提示 和 Rails validation error message 相关的合集
2013-07-15 21:05
531 查看
首先,有这样一个简单的问题,如果你想汉化,数字(不是长度)不能大于多少的model错误提示信息,怎么做?
那么,先说验证是validates_numericality_of而数字大于多少的话,应该用:greater_than_or_equal_to
如果是希望了解,类似上面的信息的话,可以参考下面
Active Record Validations and Callbacks
而,我想说的是,如果你添加了
Ruby代码
validates_numericality_of
:greater_than_or_equal_to
之后,当然就有错误提示了,可是你不想要系统的错误提示那么怎么办?
当然,最方便的办法是
Ruby代码
: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代码
your_app/vendor/plugins/new_errors/lib
3.在“new_errors”下建立一个init.rb“文件,内容为:
Ruby代码
require "new_errors"
4.在”lib“下新增一个叫”new_errors.rb“的文件。
内容如下:
Ruby代码
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将自动被装入。
问题只剩一个了,没有针对数字的错误提示:
实际上,是有的应该参考
Ruby代码
ActiveRecord::Errors.default_error_messages
Ruby代码
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要写提示:
Ruby代码
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
那么,先说验证是validates_numericality_of而数字大于多少的话,应该用:greater_than_or_equal_to
如果是希望了解,类似上面的信息的话,可以参考下面
Active Record Validations and Callbacks
而,我想说的是,如果你添加了
Ruby代码
validates_numericality_of
:greater_than_or_equal_to
之后,当然就有错误提示了,可是你不想要系统的错误提示那么怎么办?
当然,最方便的办法是
Ruby代码
: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代码
your_app/vendor/plugins/new_errors/lib
3.在“new_errors”下建立一个init.rb“文件,内容为:
Ruby代码
require "new_errors"
4.在”lib“下新增一个叫”new_errors.rb“的文件。
内容如下:
Ruby代码
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将自动被装入。
问题只剩一个了,没有针对数字的错误提示:
实际上,是有的应该参考
Ruby代码
ActiveRecord::Errors.default_error_messages
Ruby代码
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要写提示:
Ruby代码
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
相关文章推荐
- 如何修改默认的ModelState错误提示:字段{0}必须是一个数字
- WeX5中如何去掉或者修改默认网络错误提示JUSTEP230109
- new class的时候,提示以下错误: Unable to parse template "Class" Error message: This template did not produce
- 关于Rails的错误提示 Rails flash error不消失
- Struts2上传文件修改默认错误提示
- 【matlab 相关】如何修改matlab中默认的current folder
- 如何修改RubyOnRails中ActiveRecord默认的表格名和外键字段映射关系?
- vi修改编辑文件提示Write Error!(File System full?)错误的研究
- 当我new class的时候,提示以下错误: Unable to parse template "Class" Error message: This template did not produce
- Struts2上传文件修改默认错误提示
- rails中的正则表达式的相关错误提示
- 更改MVC3默认错误提示信息 模型验证 validation
- 诗歌rails之 关于Rails的错误提示 Rails flash error
- IIS配置与错误提示 500.19 - Internal Server Error 无法访问请求的页面,因为该页的相关配置数据无效 解决方法
- 点击登录Oracle企业管理器,提示错误,您的用户名和/或口令无效和如何修改oracle里面的sys和system的密码
- maven 本地仓库如何配置以及如何修改默认.m2仓库位置相关
- 如何修改Eclipse默认打开路径
- Win10系统怎么修改默认提示音?Win10修改默认提示音的方法
- SSH整合struts2中validation出现重复错误提示问题
- 如何修改默认调试器-windbg