久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
    分享

    關(guān)于Rails的錯誤提示 Rails flash error不消失

     熱血奇跡 2013-04-09
    寫下這個題目,我就想起了先賢孔乙己老人。本來的題目就想寫Rails的錯誤提示有幾種,其實,這是個看起來很簡單的題目,就像回字的寫法一樣。 

         首先,錯誤提示根據(jù)來源不同應(yīng)該分Flash 和 error_message. 

         眾所周知,flash應(yīng)該是來源于controller,這個設(shè)計靈感來源是flash ram閃存,快速和暫時存儲。稍微準(zhǔn)確的定義是,保存信息直到下一次redirect。那么,這就會遇到問題,如果,我們沒有redirect,而是render到另一個頁面,那么flash提示就會一直存在。其實,Rails為flash準(zhǔn)備以下備用選擇: 

         簡單講就是 
    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              # this is from http://  
    2. flash.discard(:warning)    # this is from http://



    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對應(yīng)的三種,類型的頁面顯示是不一樣的,假設(shè)你需求是希望能一樣顯示: 
    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_messages_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" %>  


        最后,關(guān)于errors.full_messages,本來是想說說,自己重寫的validate和系統(tǒng)自己的諸如以下驗證的先后調(diào)用關(guān)系的 
    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"]  


    借個地方用用 
    Java代碼  收藏代碼
    1. ^ +\d+.  

    Java代碼  收藏代碼
    1. mysqldump --opt --user=root --password database > file.sql  
    2. GRANT ALL PRIVILEGES ON *.* TO 'nuser'@'%' IDENTIFIED BY 'npasswd' WITH GRANT OPTION;  

      本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
      轉(zhuǎn)藏 分享 獻(xiàn)花(0

      0條評論

      發(fā)表

      請遵守用戶 評論公約

      類似文章 更多

      主站蜘蛛池模板: 精品国产一区av天美传媒| 国产成人无码免费视频在线 | 麻豆成人传媒一区二区| AAA级久久久精品无码片| 中文字幕国产精品资源| 99热国产这里只有精品9| 亚洲AV综合色区在线观看| 日本高清色WWW在线安全| 人妻久久久一区二区三区| 色爱综合激情五月激情| 成人欧美一区二区三区在线观看| 精品无码国产日韩制服丝袜| 四虎永久地址WWW成人久久| 亚洲AV无码之国产精品网址| 久久天天躁狠狠躁夜夜躁2020| 东京热人妻无码一区二区av| 国产亚洲欧美另类一区二区| 精品亚洲国产成人av| 国产99在线 | 免费| 亚洲天堂av日韩精品| 国产欧美日韩VA另类在线播放| 漂亮人妻中文字幕丝袜| 呦系列视频一区二区三区| 午夜大片爽爽爽免费影院| 色偷偷www.8888在线观看| 日日摸夜夜添狠狠添欧美| 午夜DY888国产精品影院| 中文字幕无码日韩专区免费| 国产在线超清日本一本| 樱花草视频www日本韩国| 免费久久人人爽人人爽AV| 国产日产欧产美韩系列麻豆| 精品国偷自产在线视频99| 又粗又大又黄又硬又爽免费看| 亚洲日本精品一区二区| 国产精品自产拍在线观看中文| 国产亚洲AV电影院之毛片| 午夜免费福利小电影| 色翁荡熄又大又硬又粗又视频 | 亚洲成年轻人电影网站WWW| 四虎永久免费高清视频|