The Flash

原创 2016年05月30日 17:06:19


flash.now[:error] = ""
render :new


flash[:error] = ""
redirect videos_path



http://guides.rubyonrails.org/action_controller_overview.html

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 passing error messages etc.

It is accessed in much the same way as the session, as a hash (it's a FlashHash instance).

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:

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

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

You can assign :notice:alert or the general purpose :flash:

redirect_to root_url, notice: "You have successfully logged out."
redirect_to root_url, alert: "You're stuck here!"
redirect_to root_url, flash: { referral_code: 1234 }

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 any error alerts or notices from the flash in the application's layout:

<html>
  <!-- <head/> -->
  <body>
    <% flash.each do |name, msg| -%>
      <%= content_tag :div, msg, class: name %>
    <% end -%>
 
    <!-- more content -->
  </body>
</html>

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

You can pass anything that the session can store; you're not limited to notices and alerts:

<% if flash[:just_signed_up] %>
  <p class="welcome">Welcome to our site!</p>
<% end %>

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

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
5.2.1 flash.now

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 use flash.now in the same way you use the normal flash:

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



相关文章推荐

嵌入式开发中用到的存储RAM FLASH ROM

本博客转载自http://blog.csdn.net/u013485792/article/details/50454794谢谢博主分享,请尊重版权  RAM(Random AccessMe...

头像截图上传两种方式(SWFUpload、一个简单易用的flash插件)

SWFUpload是一个客户端文件上传工具,最初由Vinterwebb.se开发,它通过整合Flash与JavaScript技术为WEB开发者提供了一个具有丰富功能继而超越传统标签的文件上传模式。另一...

html5结合flash实现视频文件在所有主流浏览器兼容播放

由于html5的出现,让网页中的视频、音频有了更加便捷的实现方式。但是video、audio标签只在IE 9+、Safari 3+、FireFox 4+、Opera 10+、Chrome 3+的浏览器...

第一篇博客 flash AS3.0 做的BrainFuck编译器

如果QQ空间和人人日志不算博客的话,那我这还是第一次写这东西。居然开通博客3天内不能发,严重打击了我的心情啊。想当年我帐号被爆导致现在邮箱里还有好多垃圾邮件的时候,我都没说什么,很不爽啊。     ...

flash上传头像插件使用说明

组件下载地址: flash上传头像剪裁预览组件_头像上传插件 - 免费开源 http://www.hdfu.net/ 参数说明: imgUrl:默认图片地址 uploadUrl:接收...

DSP28335—把TMS320F28XXX的程序段从flash复制到ram中运行

章来自:http://www.61ic.com/Article/C2000/Delfino/201303/47552.html 翻译自TI应用手册SPRAAU8   很详细,对编...

NAND FLASH编程器烧录详解

NAND FLASH编程器_NAND FLASH烧录器          SUPERPRO5000是西尔特出品的新一代USB接口独立式智能极速NAND FL...
  • broadCE
  • broadCE
  • 2015年03月02日 15:21
  • 3768

ubuntu-16.04中浏览器的配置-字体调整和flash播放器的安装

ubuntu-16.04中chromium浏览器的配置-字体调整和flash播放器的安装

如何下载浏览器中的flash 视频

首先声明,以下仅供参考,所测试所用视频资源,均为开放视频来源。 也就是说,我测试用的视频,都是任何人都可以看的。都是没有任何权限就可以看的。都是开放的视频。 准备工具,任意浏览器,我这里用的是 谷歌c...

Linux操作系统下Flash插件安装设置方法

Linux操作系统下Flash插件安装设置方法   发布时间:2008.04.14 09:44     来源:赛迪网    作者:sixth ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:The Flash
举报原因:
原因补充:

(最多只允许输入30个字)