Skip to main content

Extending Rails flash

Rails • Asked by Jiri Prochazka

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


And another question - is there an easy way to access flash from lib?


Hey Jiri,

Do you know for sure if that file is being loaded? I think that it should be since it's in app, but I would just want to verify that.

You can also just simply define this in an initializer if you want a simpler way of doing this. It would look something like this:

# config/initializers/flash.rb

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

And last question, what's the goal you're trying to accomplish with this?


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 27,623+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.