I'm using the Devise authentication gem in my Rails project, and I want to change the keys it's using in flash alerts. (Devise uses :notice and :alert flash keys, but I want to change them to :success and :error so that I can display nice green/red boxes with Bootstrap.)
So I want to be able to somehow override the set_flash_message
method in DeviseController.
Here's the new method:
def set_flash_message(key, kind, options = {})
if key == 'alert'
key = 'error'
elsif key == 'notice'
key = 'success'
end
message = find_message(kind, options)
flash[key] = message if message.present?
end
But I just don't know where to put it.
UPDATE:
Based on an answer I created a config/initializers/overrides.rb file with the following code:
class DeviseController
def set_flash_message(key, kind, options = {})
if key == 'alert'
key = 'error'
elsif key == 'notice'
key = 'success'
end
message = find_message(kind, options)
flash[key] = message if message.present?
end
end
But this causes an error on every Devise action:
Routing Error: undefined method 'prepend_before_filter' for Devise::SessionsController:Class
If you try to reopen a class, it's the same syntax as declaring a new class:
class DeviseController
end
If this code is executed before the real class declaration, it inherits from Object instead of extending the class declared by Devise. Instead I try to use the following
DeviseController.class_eval do
# Your new methods here
end
This way, you'll get an error if DeviseController
has not been declared. As a result, you'll probably end up with
require 'devise/app/controllers/devise_controller'
DeviseController.class_eval do
# Your new methods here
end
Using Rails 4 @aceofspades answer didn't work for me.
I kept getting require': cannot load such file -- devise/app/controllers/devise_controller (LoadError)
Instead of screwing around with load order of initializers I used the to_prepare
event hook without a require statement. It ensures that the monkey patching happens before the first request. This effect is similar to after_initialize
hook, but ensures that monkey patching is reapplied in development mode after a reload (in prod mode the result is identical).
Rails.application.config.to_prepare do
DeviseController.class_eval do
# Your new methods here
end
end
N.B. the rails documentation on to_prepare
is still incorrect: See this Github issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With