New Discussion

Notifications

You’re not receiving notifications from this thread.

Extending Rails flash

2
Rails

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?

Join the discussion
Create an account Log in

Learning Ruby on Rails? Join our newsletter.

We won't send you spam. Unsubscribe at any time.