Monkey patching Devise (or any Rails gem)

后端 未结 7 1387
甜味超标
甜味超标 2020-12-07 23:11

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 wa

7条回答
  •  误落风尘
    2020-12-07 23:46

    You need to overwrite DeviseController while keeping around its superclass, in your initializer.

    Something like:

    class DeviseController < Devise.parent_controller.constantize
        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
    

提交回复
热议问题