【问题标题】:How use Flash[:notice] to notify while using Backup Gem?使用 Backup Gem 时如何使用 Flash[:notice] 进行通知?
【发布时间】:2012-09-19 18:59:41
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    flash[:notice] = "User successfully created"
    

    flash[:notice] = "Some error occured"
    

    根据条件闪烁消息将显示在下一页。

    谢谢

    【讨论】:

      【解决方案2】:

      “默认情况下,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
      

      就像 Singh 所说,这些消息是为了让用户知道发生了什么。使用备份 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

      【讨论】:

        猜你喜欢
        • 2012-09-30
        • 1970-01-01
        • 2012-07-30
        • 2012-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-17
        • 1970-01-01
        相关资源
        最近更新 更多