All threads / Extending Rails flash

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Extending Rails flash

Jiri Prochazka asked in 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

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.