加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 17:41:57 所属栏目:百科 来源:网络整理
导读:写下这个题目,我就想起了先贤孔乙己老人。本来的题目就想写Rails的错误提示有几种,其实,这是个看起来很简单的题目,就像回字的写法一样。 ???? 首先 ,错误提示根据来源不同应该分Flash 和 error_message. ???? 众所周知,flash应该是来源于controller,
写下这个题目,我就想起了先贤孔乙己老人。本来的题目就想写Rails的错误提示有几种,其实,这是个看起来很简单的题目,就像回字的写法一样。

???? 首先,错误提示根据来源不同应该分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_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"?%>??


??? 最后,关于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,?: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; ?


转自:http://hlee.iteye.com/blog/447437

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读