`
chinapkw
  • 浏览: 109111 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

error_messages_for and error_message_on

阅读更多

一般我们都要模型内使用validates_xxx_of 这样的确认信息。而这些信息是英文的,我们用三种方法来中文化它。


一、覆写:message:

validates_format_of :image_url,
:with => %r{.(gif|jpg|png)$}i,
:message => "必须是 GIF,JPG或PNG图片!!!"

使用这种方式时,我们就损失了一些通用信息的方便性。让我们看看第二种方式。

二、修改environment.rb文件:

在其中加入这些语句:

ActiveRecord::Errors.default_error_messages[:inclusion] = " 字段没有包括在列表内!"
ActiveRecord::Errors.default_error_messages[:exclusion] = " 字段已被保存过了!"
ActiveRecord::Errors.default_error_messages[:invalid] = " 字段是无效的!"
ActiveRecord::Errors.default_error_messages[:confirmation] = " 字段不匹配的信息!"
ActiveRecord::Errors.default_error_messages[:accepted] = " 字段必须接受此选项!"
ActiveRecord::Errors.default_error_messages[:empty] = " 字段不能为空!"
ActiveRecord::Errors.default_error_messages[:blank] = " 字段不能为空!"
ActiveRecord::Errors.default_error_messages[:too_long] = " 字段太长了(最大是 %d 个英文字符)!"
ActiveRecord::Errors.default_error_messages[:too_short] = " 字段太短了(最小是 %d 个英文字符)!"
ActiveRecord::Errors.default_error_messages[:wrong_length] = " 字段长度有错误(应该是 %d 个英文字符)!"
ActiveRecord::Errors.default_error_messages[:taken] = " 字段已经选择过了!"
ActiveRecord::Errors.default_error_messages[:not_a_number] = " 字段不是个数字!"

这样,你就获得了通用信息的方便性,即使你没有使用:message来覆写中文信息,你的view中显示的也是中文

但是我发现它有两个不便之处:一是修改时要重启WEBrick才能生效。二是在测试时会提示出错的。我用了第三种方式。

三、修改application_help.rb文件:

在该文件中加入下面语句:

ActiveRecord::Errors.default_error_messages = {
:inclusion => " 字段没有包括在列表内!" ,
:exclusion => " 字段已被保存过了!" ,
:invalid => " 字段是无效的!" ,
:confirmation => " 字段不匹配的信息!" ,
:accepted => " 字段必须接受此选项!" ,
:empty => " 字段不能为空!" ,
:blank => " 字段不能为空!" ,
:too_long => " 字段太长了(最大是 %d 个英文字符)!" ,
:too_short => " 字段太短了(最小是 %d 个英文字符)!" ,
:wrong_length => " 字段长度有错误(应该是 %d 个英文字符)!" ,
:taken => " 字段已经选择过了!" ,
:not_a_number => " 字段不是个数字!" ,
}

在Ruby On Rails里面,当你在对一些数据进行验证的时候,如果在视图里使用<%= error_messages_for “xxx” %><%= error_message_on “xxx”, “xxx” %> 方法输出错误信息的时候,ror会自动帮你加上<div class=”xxx”>xxx</div>等这样的HTML标签,可是有时候我们并不想使用系统默认的标签处理,尤其是在WAP编程的时候,因为这些HTML标签WML根本就解析不了,所以我就希望不输出这些标签,只要得到干净的错误信息就行了,其实实现这种思路很简单,首先:
1:在application_helper.rb的全局转helper类里定义如下方法:
 
  def clear_error_message_on(object, method, prepend_text = “”, append_text = “”, css_class = “formError”)
    if errors = instance_variable_get(”@#{object}”).errors.on(method)
      “#{prepend_text}#{errors.is_a?(Array) ? errors.first : errors}#{append_text}”
    end
  end
 
2:然后在需要显示错误信息的视图里调用这个方法就行,如:
<%= clear_error_message_on “page”, “title” %>

评论

相关推荐

    message_block:替代error_messages_for,功能更强大,更灵活

    该视图助手通过从模型中获取错误消息并将其与Flash消息(错误,确认等多种类型)组合并输出到视图中,从而替代了error_messages_for。 该gem附带示例样式表和图像。 安装:Rails 3.1+ Asset Pipeline(message_...

    Oracle9i Database Error Messages

    Oracle9i Database Error Messages Release 2 (9.2) Contents Title and Copyright Information Send Us Your Comments Preface Audience Organization Related Documentation Conventions Documentation ...

    手机游戏开发实例(smartphone联机炸弹人)

    Windows Programming and Message Handling essentials ...........................................................................8 II.1.3. Drawing Bitmaps to the screen.................................

    DC错误信息注释_design complier error report annotations

    Synthesis Error Messages, uid UID-1 (error) Link command is not available. UID-2 (error) There are no designs to be linked UID-3 (warning) Can’t read link_library file ’%s’ UID-4 (error) Current ...

    Error Messages 10.2.pdf

    oracle10.2错误信息,有问题时查一查是很有用的。

    Spring 3 MVC 国际化 ResourceBundleMessageSource spring:message

    一个简单的基于Maven 3 和...error_messages_en.properties error_messages_zh.properties 没有库文件,maven配好了会自动加载库文件。也可自己搭建好,拷贝文件过去运行。可参考http://www.docin.com/p-467977489.html

    COMSOL Error messages

    COMSOL错误提示信息总结,收集至COMSOL帮助文件

    ttoolly:Django测试工具

    {'field1':(value1,value2)} custom_error_messages {} 特定字段的自定义消息custom_error_messages = {'field1':{message_type:u“错误消息文本。”}} 错误[] 不覆盖(存储当前测试的错误值) 档案[] 当前...

    [绝版文档] Error and Event Messages Help 错误及事件消息 微软官方文档 CHM [免费版]

    Error and Event Messages Help 错误及事件消息 微软官方文档 CHM [免费版] 此帮助文件包含由Windows 2000所产生的大多数错误和事件消息。对每个消息,有详细的解释及建议用户操作。 This Help file contains most...

    8-07-14_MegaCLI for linux_windows

    SCGCQ00341724 Defect FW allows a firmware to be flashed through Online FW Update method in DOS and then breaks into montask SCGCQ00342848 Defect Add Adapter restart support in CLI SCGCQ00342993 Defect...

    Senfore_DragDrop_v4.1

    NO_WIN32_LEAN_AND_MEAN The define *must* be made in the project options. It is not sufficient to #define it in the source. If you manage to compile with C++ Builder (any version), I would very ...

    Clustering by Passing Messages Between Data Points

    Clustering data by identifying a subset of representative examples is important for processing sensory signals and detecting patterns in data. Such “exemplars” can be found by randomly choosing an ...

    ajv-error-messages

    var normalise = require ( 'ajv-error-messages' ) ; var SchemaValidator = require ( 'ajv' ) ; var schemaValidator = SchemaValidator ( { allErrors : true } ) ; var schema = { description : 'test schema...

    php.ini-development

    On = Enabled and buffer is unlimited. (Use with caution) ; Off = Disabled ; Integer = Enables the buffer and sets its maximum size in bytes. ; Note: This directive is hardcoded to Off for the CLI ...

    BURNINTEST--硬件检测工具

    - Minor changes to the No operation error watchdog timer for the CD and Hard disk tests. - Minor correction to the Butterfly seek test. - Video playback trace logging increased. Release 5.3 build ...

    Sending Email using MAPI - A COM DLL(sending email demo and soucecode)

    It can be used to send emails automatically with customized user messages and attachments to specific people at particular time, especially while using some exe servers or NT services. That's it. ...

    Debugging Malloc Lab: Detecting Memory-Related Errors

    You may find that this global list can also allow you to be more specific in your error messages, as it is otherwise difficult to determine the difference between an overwrite of a non-payload area ...

    KafkaOffsetMonitor监控工具2017年1月发布的版本

    Improved createNewAdminClient code, simplifying the error paths and property calling close on error. Re-factored some of the error handling paths, simplifying them. Closing all kafka clients on error ...

    Business Objects Designer Online Guides

    The following online guides provide complete information for using Designer and other useful information. You can access each of these guides from the product documentation library. All guides are in...

    JLink_Windows_V648.zip

    DLL: Windows: Renesas RX: When using FINE interface and disabling ongoining debug mode on debug session close, it could happen that a thread was not exited gracefully, causing handle leaks. Fixed. DLL...

Global site tag (gtag.js) - Google Analytics