Ruby页面错误提示flash

http://guides.rubyonrails.org/v3.0.8/action_controller_overview.html#the-flash

如何在页面显示controller里的错误提示,

The flash is a special part of the session which is cleared with each request. This means that values stored there will only
be available in the next request, which is useful for storing error messages etc. It is accessed in much the same way as the session, like a hash. Let’s use the act of logging out as an example. The controller can send a message which will be displayed to
the user on the next request:

flash是session特殊的一部分,每次请求的时候都会被清除,这意味着存储的数据只有在下一次请求的过程中才有效.这一特性被广泛的应用于存储错误信息提示,获取它的方式与session近似,类似于哈希.让我们以退出为例子,controller发送的信息将在用户下次请求的时候展示出来.

class LoginsController < ApplicationController
  def destroy
    session[:current_user_id] = nil
    flash[:notice] = "You have successfully logged out"
    redirect_to root_url
  end
end

Note it is also possible to assign a flash message as part of the redirection.

注意:也可以在页面重定向的时候指定flash的信息

redirect_to root_url, :notice => "You have successfully logged out"

The destroy action redirects to the application’s root_url, where the message will be displayed. Note that it’s entirely up to the next action to decide what, if anything, it
will do with what the previous action put in the flash. It’s conventional to display eventual errors or notices from the flash in the application’s layout:

注销登录的操作会使得页面重定向到根目录,这时候信息也会显示在页面上.这完全取决于下一步操作是什么,总之,上一次请求的信息将会被存入flash,通常将会把最终的错误信息或者提示信息展示到页面上.

<html>
  <!-- <head/> -->
  <body>
    <% if flash[:notice] %>
      <p class="notice"><%= flash[:notice] %></p>
    <% end %>
    <% if flash[:error] %>
      <p class="error"><%= flash[:error] %></p>
    <% end %>
    <!-- more content -->
  </body>
</html>

This way, if an action sets an error or a notice message, the layout will display it automatically.

通过这种方式, 如果操作过程中出现的错误提示或者通知,会自动的加载到页面上

If you want a flash value to be carried over to another request, use the keep method:

如果你想把提示信息带到其他请求中使用,使用keep方法

class MainController < ApplicationController
  # Let‘s say this action corresponds to root_url, but you want
  # all requests here to be redirected to UsersController#index.
  # If an action sets the flash and redirects here, the values
  # would normally be lost when another redirect happens, but you
  # can use ‘keep‘ to make it persist for another request.
  def index
    # Will persist all flash values.
    flash.keep

    # You can also use a key to keep only some kind of value.
    # flash.keep(:notice)
    redirect_to users_url
  end
end

By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the create action fails to save a resource and
you render the new template directly, that’s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can useflash.now in
the same way you use the normal flash:

默认状态下, flash里的值会在下一次请求的时候有效, 但是有的时候,你可能希望在同一个请求里使用这些值,比如新建的时候保存数据失败并跳转到一个新的页面里,flash不会出现在这个新的请求里.如果依然希望通过flash来显示错误信息,可以直接使用flash.now方法.

class ClientsController < ApplicationController
  def create
    @client = Client.new(params[:client])
    if @client.save
      # ...
    else
      flash.now[:error] = "Could not save client"
      render :action => "new"
    end
  end
end

欢迎来到我的博客, 本人乃互联网行业一枚小小的螺丝钉—非典型程序员妹子.

留下微信地址,方便大家和我联系

时间: 2024-08-07 03:17:08

Ruby页面错误提示flash的相关文章

谷歌浏览器Chrome错误提示Flash过期怎么办(转)

在使用谷歌浏览器Chrome时,会碰到谷歌浏览器Chrome的错误提示:"Adobe Flash Player因过期而遭到阻止",点击"更新插件"是不行的,国内的网络根本就打不开,点击"运行一次"是可以的,但是每次都要点,这样就变得很麻烦.那么,当谷歌浏览器Chrome出现错误提示:"Adobe Flash Player因过期而遭到阻止"时该怎么办呢?请看: 工具/原料 谷歌浏览器Chrome Adobe Flash Play

ie8页面错误提示:Unable to modify the parent container element before the child element is closed (KB927917)

Windows xp  sp3 Unable to modify the parent container element before the child element is closed (KB927917) PowerPointFrame.aspx 安装  KB2416400补丁,搞定 IE8补丁:http://www.microsoft.com/zh-cn/download/details.aspx?id=10152

IIS:打开任意 .aspx 页面,提示 404 错误

环境:win2003 sp2 x64 + iis 6.0 + asp.net 2.0 问题描述 IIS 打开任意 .aspx 页面,提示 404 错误 无法找到该页 问题解决 IIS 的 "Web 服务扩展",找到"ASP.NET v2.0.50727"项,设置为"允许". 如果找不到"ASP.NET v2.0.50727"项,可能是: 1.未安装 .NET Framework 2.0,请先安装: 2..net2.0已经安装,

JSP中,当页面为404或者500时。设置跳转到错误提示页面

最好的就是在WEB.XML文件中配置错误代码的跳转页面,首先建立个 出现500错误的页面,提示出错了,然后再WEB.XML文件中配置,配置如下 一. 通过错误码来配置error-page <error-page> <error-code>404</error-code> <location>/NotFound.jsp</location> </error-page> <error-page> <error-code&

关于登录或授权页面的错误提示

写这篇文章是由于最近在产品登录授权页面上,到底如何返回错误提示引发的一些思考,调研后做下总结. 登录和授权在现在的互联网产品中属于最常用必备的模块,在互联网安全形势严峻的今天,其重要性更是不言而喻.如何处理好这个部分,关系到产品在用户心里的信誉.关于登录授权的安全性涉及方方面面,本文重点讨论的场景是:当登录或授权失败时,应该如何返回给用户错误提示. 场景及问题 场景具体描述为: 用户进入登录页面,输入手机号,点击发送短信验证码:用户收到短信验证码后,输入验证码,点击登录:校验信息成功,给予用户授

ie的开发者工具在页面包含iframe,且src不同域时,由打开开发工具而触发的js错误提示

经过测试,得出以下结论: 我认为是ie的按f12时,打开的那个开发工具本身的在访问某些资源时触发的错误,跟网站没有任何关系. 按照我的猜测: 也就是这个开发工具访问了某些资源,而且这个访问方式是使用js的方式来访问的,而这个访问也遵守应用到某些网站上的访问审核流程;这个开发工具,也没有特殊的权力越过这个审核流程,最后触发了出错提示; 结论来源重现步骤(域名的准备可以修改window的hosts来达到) 1. url :准备域名,http://l.com/,且域名下http://l.com/ind

“网站内容可以显示但在页面底部提示错误”的解决方法

“网站内容可以显示但在页面底部提示错误”的解决方法 今天阿D遇到一个问题,客户网站内容可以正常访问,但是网站尾部都出现数据库等一大推错误提示,如下图 起初以为是网站权限问题,设置好权限,还是提示这个错误,然后又去纠结数据库文件和数据库配置文件是否修改好,都检查无误,依旧提示这个错误,最后根据那个warning的路径,发现tplcache 下好多缓存文件,然后把这些在这个目录下新建个目录,把所有缓存文件拖进新建的那个目录,再次访问,就正常了 总结:网站可以正常打开,多了一堆后缀,首先要去看错误提示

java配置ueditor中解决“未找到上传文件”错误提示

ueditor是一个功能十分强大的在线文本编辑器,但是在ssh框架中,确切的说实在struts2中由于其拦截器需要对request,session对象进行重新封装,这个过程中会把request对象中保存的一些内容清空,所以会导致ueditor的上传功能获取不到需要上传的内容导致“未找到上传文件”的错误! 参考网上资料和自己实验,最终的解决思路是,重写struts2中的一个转换的类,然后配置struts2使用我们重写的这个类.由于我们的工程中可能会有其他的上传等功能,为了不影响其他功能的时候,还需

解决Asp.net中的Chart控件运行出现错误提示“ ChartImg.axd 执行子请求时出错”

首先经过错误提示看出需要保存路径之类的,所以猜测是不是配置文件出错了.看了一下配置文件只有连接字符串的配置.后来重新把vs2010关了重新打开,又新建了一个页面,重新添加空间然后运行使用.然后再看配置文件,发现多了许多东西.具体没有配置的东西如下: <?xml version="1.0"?> <!-- 有关如何配置 ASP.NET 应用程序的详细消息,请访问 http://go.microsoft.com/fwlink/?LinkId=169433 --> &l