什么是红宝石轨道上的闪存散列 [英] What is flash hash in ruby on rails

查看:177
本文介绍了什么是红宝石轨道上的闪存散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名经验丰富的PHP开发人员,但是刚刚接触RoR并试图了解每件事情的工作原理。我知道如何使用闪存散列,即在我的行动方法,我会设置

  flash [:notice] ='一些消息'

在我看来,我会显示。
这个机制也在Yii框架中实现。我明白它是如何工作的。我不明白的是它在RoR中的实际工作原理。 flash 仅仅是一个局部变量,那么我怎样才能在我的视图中访问它?

解决方案

flash 实际上是一种方法。这不是在你的控制器,但Rails控制器委托它的请求对象。因此,在请求对象中定义了 flash 方法,但您可以从控制器和视图中访问它。

检查 ActionDispatch :: Request 代码。实际上,闪存实际上存储在会话中。用户下一次请求页面时,可以在视图中使用Flash。



在您的视图中,您可以像这样访问它:

 <%= flash [:notice]%> 

或者以更美观的方式(这只能通过注意和警告来完成,因为它们是经常使用):

 <%= flash.notice%> 

请参阅文档了解更多信息。


I am an experienced PHP developer but new to RoR and trying to understand how every thing works. I know how to use flash hash i.e in my action method, I'll set

flash[:notice] = 'some message'

and in my view, i'll display that. This mechanism is also implemented in Yii framework. I understand how it works there. The thing that I don't understand is how it actually works here in RoR. flash is just a local variable then how can I access it in my views?

解决方案

flash is actually a method. It's not in your controller, but the Rails controller delegates it to the request object. So the flash method is defined in the request object, but you can access it from your controllers, and from the view.

Check the link to the ActionDispatch::Request code. The flash is actually stored inside the session when set. The next time the user requests a page, the flash is accessible for use in the views.

In your view you can just access it like this:

<%= flash[:notice] %>

Or in a aesthetically more pleasant way (this can only be done with notice and alert since they're so frequently used):

<%= flash.notice %>

See the documentation for more information.

这篇关于什么是红宝石轨道上的闪存散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆