Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What flash message types are available in Rails?

So far I've got:

  • :notice
  • :alert
  • :error

but is there definitive list, that can be used in place, such as in redirect_to path, :error => "Oh no!"?

like image 250
cjm2671 Avatar asked Jan 30 '12 12:01

cjm2671


People also ask

What are flash messages in Rails?

What are flash messages? A flash message is a way to communicate information with the users of your Rails application so they can know what happens as a result of their actions. Example messages: “Password changed correctly” (confirmation)

How do I use flash messaging?

To implement flash messages in NodeJs with connect-flash module, you need to install the required dependencies using the command. Express: Required by the library connect-flash to run. Express-session: to create a session whenever a message is flashed, so that the user can be redirected to a particular page.

What is flash hash?

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.


1 Answers

Hauleth is correct that you can use any symbol, but right now, :notice and :alert are the only ones you can pass directly into redirect_to (according to flash.rb in Rails source), as you specifically mention:

redirect_to path, :error => "Oh no!" # Will not work 

If you want a different flash type such as :error (or :success), you must pass those in through the :flash key, like so:

redirect_to path, :flash => { :error => "Oh no!" } 

For information on how to register your custom flash types so that, like :notice and :alert, you can pass them directly in to redirect_to, see this StackOverflow Q&A: https://stackoverflow.com/a/3848759/995663

Update: According to this commit, it seems Rails 4 will make this easier by allowing you to register custom flash types by calling add_flash_types :error in ApplicationController.

like image 120
Steve Grossi Avatar answered Sep 30 '22 23:09

Steve Grossi