标签:des style blog http color io os ar 使用
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
-----------------------------------------------------------------------------------
欢迎来到我的博客, 本人乃互联网行业一枚小小的螺丝钉—非典型程序员妹子.
留下微信地址,方便大家和我联系
标签:des style blog http color io os ar 使用
原文地址:http://www.cnblogs.com/iwangzheng/p/4043639.html