Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow custom flash keys in a redirect_to call in Rails 3

In Rails 3, you can pass has attributes directly to redirect_to to set the flash. For example:

redirect_to root_path, :notice => "Something was successful!"

However, this only works with the :alert and :notice keys; if you want to use custom keys, you have to use a more verbose version:

redirect_to root_path, :flash => { :error => "Something was successful!" }

Is there any way to make it so that custom keys (such as :error, above) can be passed to redirect_to without specifying it in :flash => {}?

like image 717
Michelle Tilley Avatar asked Oct 03 '10 05:10

Michelle Tilley


1 Answers

In Rails 4 you can do this

class ApplicationController < ActionController::Base
  add_flash_types :error, ...

and then somewhere

redirect_to root_path, error: 'Some error'

http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013

like image 123
woto Avatar answered Sep 30 '22 04:09

woto