关于Rails的错误提示 Rails flash error不消失

写下这个题目,我就想起了先贤孔乙己老人。本来的题目就想写Rails的错误提示有几种,其实,这是个看起来很简单的题目,就像回字的写法一样。

[size=x-large]首先[/size],错误提示根据来源不同应该分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’].

[size=x-large]其次[/size],flash对应的三种,类型的页面显示是不一样的,假设你需求是希望能一样显示:
FLASH_NAMES = [:notice, :warning, :message]

<% for name in FLASH_NAMES %>
<% if flash[name] %>
<%= "<div id=\"#{name}\">#{flash[name]}</div>" %>
<% end %>
<% end %>



[size=x-large]然后[/size],简单说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" %>


[size=x-large]最后[/size],关于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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值