诗歌rails之 关于Rails的错误提示 Rails flash error

首先,错误提示根据来源不同应该分Flash 和 error_message.

     众所周知,flash应该是来源于controller,这个设计灵感来源是flash ram闪存,快速和暂时存储。稍微准确的定义是,保存信息直到下一次redirect。那么,这就会遇到问题,如果,我们没有redirect,而是render到另一个页面,那么flash提示就会一直存在。其实,Rails为flash准备以下备用选择:

     简单讲就是
Ruby代码
  1.  flash.discard(:error)  
  2. flash.now(:error)  
  3. flash.keep(:error)  


分别用来指定和改变flash的存活时间。具体参考下面:         
discard(k = nil)
Marks the entire flash or a single flash entry to be discarded by the end of the current action:

Ruby代码
  1. flash.discard              # discard the entire flash at the end of the current action  
  2. flash.discard(:warning)    # discard only the "warning" entry at the end of the current action  



keep(k = nil)
Keeps either the entire current flash or a specific flash entry available for the next action:

Ruby代码
  1. flash.keep            # keeps the entire flash  
  2. flash.keep(:notice)   # keeps only the "notice" entry, the rest of the flash is discarded  



now()
Sets a flash that will not be available to the next action, only to the current.

Ruby代码
  1. flash.now[:message] = "Hello current action"  

This method enables you to use the flash as a central messaging system in your app. When you need to pass an object to the next action, you use the standard flash assign ([]=). When you need to pass an object to the current action, you use now, and your object will vanish when the current action is done.

Entries set via now are accessed the same way as standard entries: flash[‘my-key’].

        其次,flash对应的三种,类型的页面显示是不一样的,假设你需求是希望能一样显示:
Ruby代码
  1. FLASH_NAMES = [:notice:warning:message]  
  2.   
  3. <% for name in FLASH_NAMES %>  
  4.   <% if flash[name] %>  
  5.     <%= "<div id=\"#{name}\">#{flash[name]}</div>" %>  
  6.   <% end %>  
  7. <% end %>  



         然后,简单说error_message
         我想说的是
Ruby代码
  1.    error_message_on  
  2. error_message_for  
  3. error.full_message  


         无疑,error_message来源Model最常见使用如下:
Ruby代码
  1. <% form_for :person:url => { :action => "update" } do |f| %>  
  2.   <%= f.error_messages %>  
  3.   First name: <%= f.text_field :first_name %><br />  
  4.   Last name : <%= f.text_field :last_name %><br />  
  5.   Biography : <%= f.text_area :biography %><br />  
  6.   Admin?    : <%= f.check_box :admin %><br />  
  7. <% end %>  


error_message_on如下:
Ruby代码
  1. <%= error_message_on "post""title" %>  
  2. # => <div class="formError">can't be empty</div>  
  3.   
  4. <%= error_message_on @post:title %>  
  5. # => <div class="formError">can't be empty</div>  
  6.   
  7. <%= error_message_on "post""title",  
  8.     :prepend_text => "Title simply ",  
  9.     :append_text => " (or it won't work).",  
  10.     :css_class => "inputError" %>  


    最后,关于errors.full_messages,本来是想说说,自己重写的validate和系统自己的诸如以下验证的先后调用关系的
Ruby代码
  1. validates_numericality_of :start_freq:greater_than_or_equal_to => 0, :allow_nil =>true:only_integer => true:less_than => 1500000001  
  2. validates_numericality_of :stop_freq:less_than => 1500000001, :allow_nil =>true:only_integer => true:greater_than_or_equal_to => 0  
  3. validates_presence_of   :region_id,  


先写简单用法吧
Ruby代码
  1. class Company < ActiveRecord::Base  
  2.   validates_presence_of :name:address:email  
  3.   validates_length_of :name:in => 5..30  
  4. end  
  5.   
  6. company = Company.create(:address => '123 First St.')  
  7. company.errors.full_messages # =>  
  8.   ["Name is too short (minimum is 5 characters)""Name can't be blank""Address can't be blank"]  
posted @ 2009-08-18 11:17  麦飞  阅读(1619)  评论(0编辑  收藏  举报