寫下這個題目,我就想起了先賢孔乙己老人。本來的題目就想寫Rails的錯誤提示有幾種,其實,這是個看起來很簡單的題目,就像回字的寫法一樣。
首先,錯誤提示根據(jù)來源不同應(yīng)該分Flash 和 error_message.
眾所周知,flash應(yīng)該是來源于controller,這個設(shè)計靈感來源是flash ram閃存,快速和暫時存儲。稍微準(zhǔn)確的定義是,保存信息直到下一次redirect。那么,這就會遇到問題,如果,我們沒有redirect,而是render到另一個頁面,那么flash提示就會一直存在。其實,Rails為flash準(zhǔn)備以下備用選擇:
簡單講就是
- flash.discard(:error)
- flash.now(:error)
- 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:
- flash.discard
- flash.discard(:warning)
keep(k = nil) Keeps either the entire current flash or a specific flash entry available for the next action:
- flash.keep
- flash.keep(:notice)
now() Sets a flash that will not be available to the next action, only to the current.
- 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è)你需求是希望能一樣顯示:
- FLASH_NAMES = [:notice, :warning, :message]
-
- <% for name in FLASH_NAMES %>
- <% if flash[name] %>
- <%= "<div id=\"#{name}\">#{flash[name]}</div>" %>
- <% end %>
- <% end %>
然后,簡單說error_message 我想說的是
- error_message_on
- error_messages_for
- error.full_message
無疑,error_message來源Model最常見使用如下:
- <% form_for :person, :url => { :action => "update" } do |f| %>
- <%= f.error_messages %>
- First name: <%= f.text_field :first_name %><br />
- Last name : <%= f.text_field :last_name %><br />
- Biography : <%= f.text_area :biography %><br />
- Admin? : <%= f.check_box :admin %><br />
- <% end %>
error_message_on如下:
- <%= error_message_on "post", "title" %>
-
-
- <%= error_message_on @post, :title %>
-
-
- <%= error_message_on "post", "title",
- :prepend_text => "Title simply ",
- :append_text => " (or it won't work).",
- :css_class => "inputError" %>
最后,關(guān)于errors.full_messages,本來是想說說,自己重寫的validate和系統(tǒng)自己的諸如以下驗證的先后調(diào)用關(guān)系的
- validates_numericality_of :start_freq, :greater_than_or_equal_to => 0, :allow_nil =>true, :only_integer => true, :less_than => 1500000001
- validates_numericality_of :stop_freq, :less_than => 1500000001, :allow_nil =>true, :only_integer => true, :greater_than_or_equal_to => 0
- validates_presence_of :region_id,
先寫簡單用法吧
- class Company < ActiveRecord::Base
- validates_presence_of :name, :address, :email
- validates_length_of :name, :in => 5..30
- end
-
- company = Company.create(:address => '123 First St.')
- company.errors.full_messages
- ["Name is too short (minimum is 5 characters)", "Name can't be blank", "Address can't be blank"]
借個地方用用
- mysqldump --opt --user=root --password database > file.sql
- GRANT ALL PRIVILEGES ON *.* TO 'nuser'@'%' IDENTIFIED BY 'npasswd' WITH GRANT OPTION;
|