What flash message types are available in Rails?

Ruby on-Rails

Ruby on-Rails Problem Overview


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!"?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

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.

Solution 2 - Ruby on-Rails

No, as a flash type you can use any symbol, even your own.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncjm2671View Question on Stackoverflow
Solution 1 - Ruby on-RailsSteve GrossiView Answer on Stackoverflow
Solution 2 - Ruby on-RailsHaulethView Answer on Stackoverflow