论坛首页 编程语言技术论坛

可定制的Rails错误回显

浏览 11229 次
精华帖 (1) :: 良好帖 (13) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-09-17  
Hooopo 写道
amonlei 写道
很明显,楼主没有去阅读过 rails文档

文档都是用来查的.....
原来是error_message_on啊!
引用

error_message_on(object, method, *args)
Returns a string containing the error message attached to the method on the object if one exists. This error message is wrapped in a DIV tag, which can be extended to include a :prepend_text and/or :append_text (to properly explain the error), and a :css_class to style it accordingly. object should either be the name of an instance variable or the actual object. The method can be passed in either as a string or a symbol. As an example, let’s say you have a model @post that has an error message on the title attribute:

  <%= error_message_on "post", "title" %>
  # => <div class="formError">can't be empty</div>

  <%= error_message_on @post, :title %>
  # => <div class="formError">can't be empty</div>

  <%= error_message_on "post", "title",
      :prepend_text => "Title simply ",
      :append_text => " (or it won't work).",
      :css_class => "inputError" %>




<%= error_message_on "post", "title", 
      :prepend_text => "Title simply ", 
      :append_text => " (or it won't work).", 
      :css_class => "inputError" %> 

啊,土了,,原来error_message_on这个轮子已经造好了。
貌似ActionView::Base.field_error_proc也很不错。

文档不是用来查的,平时阅读以下,知道有哪些方面的内容,不然真是书到用时方恨少
1 请登录后投票
   发表时间:2009-09-20  
amonlei 写道

8.3 Customizing the Error Messages HTML

By default, form fields with errors are displayed enclosed by a div element with the fieldWithErrors CSS class. However, it’s possible to override that.

The way form fields with errors are treated is defined by ActionView::Base.field_error_proc. This is a Proc that receives two parameters:

    * A string with the HTML tag
    * An instance of ActionView::Helpers::InstanceTag.

Here is a simple example where we change the Rails behaviour to always display the error messages in front of each of the form fields with errors. The error messages will be enclosed by a span element with a validation-error CSS class. There will be no div element enclosing the input element, so we get rid of that red border around the text field. You can use the validation-error CSS class to style it anyway you want.
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| if instance.error_message.kind_of?(Array) %(#{html_tag}<span class="validation-error">&nbsp; #{instance.error_message.join(',')}</span>) else %(#{html_tag}<span class="validation-error">&nbsp; #{instance.error_message}</span>) end end

This will result in something like the following:



Validation error messages

 

 

非常棒!这样以后再写模型验证的时候,就可以把同一类型的写到一块了。

以前总是这么写

validates_numericality_of :weight, :message => "重量 必须是数字"
validates_numericality_of :num, :message => "购买数量 必须是数字"

 虽然验证的内容是一个类型的,但是因为字段名不一样,所以还得自己加上,现在可以全部加到一起了

validates_numericality_of :num, :weight, :message => "必须是数字"

 反正错误信息会出现到表单元素的后面,方便很多,呵呵

1 请登录后投票
   发表时间:2009-09-23   最后修改:2009-09-23

class User < ActiveRecord::Base
   validates_presence_of     :email,  :message => "邮箱不能为空!"
   ......
end

 %tr
     %td{ :height => "30", :align => "right", :valign => "middle" }
          %strong{:style =>"color: red;"}*
           E-mail:
      %td{ :height => "30", :align => "left", :valign => "middle" }
           = user_form.text_field :email , :size => "20"
            %span{:style =>"color: red;"}
            = @user.errors[:email]

效果如下:

 

 

不知,大家觉得如何?

  • 大小: 807 Bytes
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics