Search code examples
ruby-on-railsmonkeypatchingextendingflash-message

Extending Rails flash


I have in app/lib/action_dispatch/flash/flash_hash.rb file this code:

class ActionDispatch::Flash::FlashHash
  def my_method
    #...
  end
end

but when I call it in controller, I get undefined method 'my_method' for ActionDispatch::Flash::FlashHash:0x007fcf8e81e510:

def index
  flash.my_method
end

Why? Any ideas?
Thanks


Solution

  • You need to require the file on startup, try using an initializer:

    # config/initializers/flash_monkeypatch.rb
    
    require "#{Rails.root}/lib/action_dispatch/flash/flash_hash"
    

    And restart your app.