关于Rails的错误提示 Rails flash error不消失
程序员文章站
2022-05-19 22:24:30
...
写下这个题目,我就想起了先贤孔乙己老人。本来的题目就想写Rails的错误提示有几种,其实,这是个看起来很简单的题目,就像回字的写法一样。
首先,错误提示根据来源不同应该分Flash 和 error_message.
众所周知,flash应该是来源于controller,这个设计灵感来源是flash ram闪存,快速和暂时存储。稍微准确的定义是,保存信息直到下一次redirect。那么,这就会遇到问题,如果,我们没有redirect,而是render到另一个页面,那么flash提示就会一直存在。其实,Rails为flash准备以下备用选择:
简单讲就是
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 # discard the entire flash at the end of the current action 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:
flash.keep # keeps the entire flash 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.
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对应的三种,类型的页面显示是不一样的,假设你需求是希望能一样显示:
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" %> # => <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" %>
最后,关于errors.full_messages,本来是想说说,自己重写的validate和系统自己的诸如以下验证的先后调用关系的
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"]
借个地方用用
^ +\d+.
mysqldump --opt --user=root --password database > file.sql GRANT ALL PRIVILEGES ON *.* TO 'nuser'@'%' IDENTIFIED BY 'npasswd' WITH GRANT OPTION;
http://tcgglobal.na4.acrobat.com
上一篇: 学弟买票经典搞笑
下一篇: 强悍的ip格式 正则表达式验证