Accessing rails flash[:notice] in a model

titaniumdecoy picture titaniumdecoy · Apr 23, 2010 · Viewed 20.2k times · Source

I am trying to assign a message to flash[:notice] in a model observer.

This question has already been asked: Ruby on Rails: Observers and flash[:notice] messages?

However, I get the following error message when I try to access it in my model:

undefined local variable or method `flash' for #<ModelObserver:0x2c1742c>

Here is my code:

class ModelObserver < ActiveRecord::Observer
  observe A, B, C

  def after_save(model)
    puts "Model saved"
    flash[:notice] = "Model saved"
  end
end

I know the method is being called because "Model saved" is printed to the terminal.

Is it possible to access the flash inside an observer, and if so, how?

Answer

Ryan Bigg picture Ryan Bigg · Apr 24, 2010

No, you set it in the controller where the saving is occurring. flash is a method defined on ActionController::Base.