Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:21:40+00:00 2026-05-16T04:21:40+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:21:41+00:00Added an answer on May 16, 2026 at 4:21 am

    I needed to set flash[:notice] in the model to override the generic "@model was successfully updated".

    This what I did

    1. Created a virtual attribute in the respective model called validation_message
    2. Then I set the virtual attribute in the respective model when needed
    3. Used an after_action when this virtual attribute was not blank to override the default flash

    You can see my controller and model how I accomplished this below:

    class Reservation < ActiveRecord::Base
    
      belongs_to :retailer
      belongs_to :sharedorder
      accepts_nested_attributes_for :sharedorder
      accepts_nested_attributes_for :retailer
    
      attr_accessor :validation_code, :validation_messages
    
      validate :first_reservation, :if => :new_record_and_unvalidated
    
      def new_record_and_unvalidated
        if !self.new_record? && !self.retailer.validated?
          true
        else
          false
        end
      end
    
      def first_reservation
        if self.validation_code != "test" || self.validation_code.blank?
          errors.add_to_base("Validation code was incorrect") 
        else
          self.retailer.update_attribute(:validated, true)
          self.validation_message = "Your validation is successful and you will not need to do that again"
        end
      end
    end
    
    class ReservationsController < ApplicationController
    
      before_filter :authenticate_retailer!
      after_filter :validation_messages, :except => :index
    
      def validation_messages
        return unless @reservation.validation_message.present?
    
        flash[:notice] = @reservation.validation_message
      end
    end
    

    One possible refactor would be to move the actual message in a proper file (e.g. a locale) and pass to validation_message only the proper key.

    Should you need more than one notice it’s easy enough to turn validation_message into an array or a hash and call it validation_messages instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to assign the onclick event to an anchor tag that already
I am trying to assign absence dates to an academic year, the academic year
I have an asp.net mvc application and i am trying to assign value to
Trying to get my css / C# functions to look like this: body {
Trying to keep all the presentation stuff in the xhtml on this project and
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
Trying to setup an SSH server on Windows Server 2003. What are some good
Trying to find some simple SQL Server PIVOT examples. Most of the examples that
Trying to make a make generic select control that I can dynamically add elements
Trying to make a MySQL-based application support MS SQL, I ran into the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.