Question

Hi I have a rails helper for collecting flash messages in engine X

def flash_messages
  message = []
  flash.each do |k,m|
    message << m
  end
  message
end

Now I have overridden this function in my main app helper

include X:Xhelper

def flash_messages
  message = []
  if required_message
    flash.each do |k,m|
      message << m
    end
  else
    message = "Flash messages are not displayed"
  end
  message
end

Now when I use this, I get the error "undefined local variable or method `flash' for Object:Class" which comes because flash is not defined for helper.

Thanks

Was it helpful?

Solution

The Rails flash is present in the context of a request, and is managed internally using part of the session. So a method that references flash must be in that context, in other words a controller. Rails helpers are, by default, available in views only, not controllers. If you move your method to ApplicationController, then it should have access to the flash.

Here's the Rails documentation on flash

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top