什么是红宝石轨道上的闪存散列 [英] What is flash hash in ruby on rails
问题描述
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屋!