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



版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

手把手教你linux下google chrome浏览器root用户无法运行,以及flash插件安装

周末终于被卡爆的win7折腾烦了,无奈之下终于下决心装了双系统,好不容易折腾好双系统,愉快的在linux下写了一个hello world后不愉快的事情终于发生了。 由于装的是kali,没有自带火狐或者...

X-LOAD从nand flash启动

X-LOAD从nand flash启动 1.前沿         刚拿到TI公司免费的开发板,发现它默认从SD卡启动,而我们产品没有SD卡,只能从NAND启动,还没有拿到产品,就用开发板来做,为以...

使用FLEX和 Actionscript 开发FLASH游戏(六)-5

使用FLEX和 Actionscript 开发FLASH游戏(六)-5使用FLEX和Actionscript开发FLASH 游戏-碰撞检测(第五页) Player.as package {     i...

Flash AS3.0 Socket资源(1)——与Socket服务器建立连接

我们在使用ActionScript3.0进行Socket编程的时候需要关注下面的问题,我们将在今后的学习中逐个对下面的问题进行讨论,并尽量逐渐的改进我们的程序.1.与Socket服务器建立连接.2.向...

NAND vs. NOR flash technology

BY ARIE TAL M-Systems Newark, CA http://www.m-sys.com NOR and NAND technologies dominate...

如何在codewarrior中的flashprogrammer添加新的flash型号

由于项目需要节约成本,更新硬件,主要包括PPC MPC5125处理器、DDR2内存以及Norflash。林工希望换掉老的spansion S29GL128P, 更新为速度更快、容量更大的spansio...

在nand flash上实现JFFS2根文件文件系统

作者:刘洪涛,华清远见嵌入式学院高级讲师,ARM公司授权ATC讲师。JFFS2是Flash上应用最广的一个日志结构文件系统。它提供的垃圾回收机制,不需要马上对擦写越界的块进行擦写,而只需要将其设置一个...

ESP8266 Flash

ESP8266 Flash 目录  [隐藏]  1 概述 1.1 系统参数区 (System param)1.2 用户参数区 (User param) 2 Layout...
  • k7arm
  • k7arm
  • 2016-07-02 18:19
  • 4229

flash 中matrix

http://help.adobe.com/zh_CN/AIR/1.1/jslr/flash/geom/Matrix.html 英文版本详细资料 http://www.senocular....

把exe的flash还原成swf

首先要准备一个16进制字节码编辑器,这里用的是UltraEdit,可以到这里下载它最新的破解版。 现在我们开始我们的还原工作吧。 1.用UltraEdit打开待还原的exe文件。你就会看到这个ex...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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