Ruby on Rails: Observers and Flash Messages [: notification]?

I am trying to send flash messages and welcome notifications to users if this is their first comment; basically something like this:

  class CommentObserver < ActiveRecord::Observer
    def after_save(comment)
      if comment.user.new?
        Mailer.deliver_welcome_package(comment)
        flash[:notice] = "Welcome! We just delivered a welcome package to your email"
      end
    end
  end

I'm not sure how I should display this flash message for users after they create their first comment. Should I put this flash message in the controller (with the optional "if comment.user.new?") Or is there a way to display the flash message more efficiently?

+3
source share
2 answers

Putting a flash message into a method seems good to me.

Usually I have a helper method in my application_helper file that checks flash and diplays.

def show_flash
    [:notice, :error, :warning].collect do |key|
      content_tag(:div, flash[key], :id => key, :class => "flash flash_#{key}") unless flash[key].blank?
    end.join
  end

, , , , - , , , .

..

<% show_flash %>
+5

-, ? , ?

, , , .

, . .

+2

Source: https://habr.com/ru/post/1727261/


All Articles