6

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?

Utku
  • 2,025
  • 22
  • 42
sadaf
  • 719
  • 2
  • 8
  • 19
  • 1
    Read http://stackoverflow.com/a/4194712/643500 and http://stackoverflow.com/a/10167659/643500 – Sully May 04 '14 at 07:04
  • Checked them but could not understand the scope of flash[:notice], If it is a local variable, how can I access it anywhere other than that action... – sadaf May 04 '14 at 07:19

3 Answers3

9

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.

fivedigit
  • 18,464
  • 6
  • 54
  • 58
0

'flash' is not a local variable. It is part of session like 'session' hash. What this implies is that session hashes (flash, session, cookies) are shared values between ActionController::Base and ActionView::Base. Therefore session hashes are accessible from controllers and from views.In order to access flash in views, just use it as you would use it in controller. Referring to your earlier code , you would print notice like this:

<% if flash[:notice]  %>
<p><%= flash[:notice] %></p>
<% end %>

For further reference on this topic please checkout: guides

0

The flash hash is basically a variable which is populated with each controller/action request, and then is reset after the request has been performed. As Adilbiy Kanzitdinov has mentioned - it's set in the session hash :

The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create action that sets flash[:notice] = "Post successfully created" before redirecting to a display action that can then expose the flash to its template. Actually, that exposure is automatically done.

You need to remember that ROR is full-stack, meaning it has a bunch of middleware it uses to create the most efficient responses for your users. This allows you to set local variables (session, params, flash are 3 I can think of)

To call from a view, you just need to reference this local variable, as below:

<%= flash[:key] %>
Richard Peck
  • 76,116
  • 9
  • 93
  • 147