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

Reply

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

Reply

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?

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.