如何在使用Backup Gem时使用Flash [:notice]进行通知?

时间:2012-09-19 18:59:41

标签: ruby-on-rails ruby database ruby-on-rails-3

我使用备份gem来创建pg DB的备份,我们可以使用notify来显示flash消息作为备份完成而不是通过Mail通知。或者任何其他定义自定义通知的方式。

2 个答案:

答案 0 :(得分:0)

当您从控制器重定向没有呈现时,Flash消息通常会有所帮助。假设您要创建用户。现在,如果创建控制器没有呈现任何内容,而是重定向到主页,则可以在此处使用Flash消息。下面给出了它的语法。

flash[:notice] = "User successfully created"

flash[:notice] = "Some error occured"

基于条件flash消息将显示在下一页。

由于

答案 1 :(得分:0)

“默认情况下,on_success,on_warning和on_failure通知始终为true。”

notify_by Mail do |mail|
  mail.on_success = false
  mail.on_error = false
  mail.on_failure = false
  #flash[:notice] = 'Done...' or whatever
end
像辛格说的那样,这些消息是让用户知道发生了什么。使用备份gem,您不需要通知用户有关备份的信息,除非它用于服务器管理员或类似的事情。

或者您可以覆盖通知

module Backup
  module Notifier
    class Mail < Base
      #....
      def notify!(status)
        name, send_log =
            case status
            when :success then [ 'Success', false ]
            when :warning then [ 'Warning', true  ]
            when :failure then [ 'Failure', true  ]
            end

        email = new_email
        email.subject = "[Backup::%s] #{@model.label} (#{@model.trigger})" % name
        email.body    = @template.result('notifier/mail/%s.erb' % status.to_s)

        if send_log
          email.convert_to_multipart
          email.attachments["#{@model.time}.#{@model.trigger}.log"] = {
            :mime_type => 'text/plain;',
            :content   => Logger.messages.join("\n")
          }
        end

        email.deliver!
      end
    end
  end
end

https://github.com/meskyanichi/backup/blob/master/lib/backup/notifier/mail.rb

相关问题