【问题标题】:Devise Confirmable - Welcome Message设计可确认 - 欢迎信息
【发布时间】:2016-07-25 23:09:00
【问题描述】:

所以我在用户注册网站时设置了欢迎消息 - 之前我使用 gmail 设置了它(http://stackoverflow.com/questions/5793296/rails-actionmailer-w-devise-google- apps-in-development-mode),但它将使用谷歌应用程序 - 所以如果我是正确的另一个stackoverflow用户声称设置是相似的,所以这不是问题。但是由于我只想要一封欢迎电子邮件,我在想我可以使用可确认设置以便他们收到一封电子邮件,然后在配置中进行设置,以便用户在说 1000 年后才需要确认大所以基本上它不是真正的确认电子邮件? (如果有更好的方法可以做到这一点,我会很感激这样的输入)

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    您无需扭曲 Confirmable 功能即可实现此目的,您可以使用ActiveRecord::Observer 更优雅地做到这一点。基本上,当您注册/保存用户时,观察者会收到通知,然后您可以从那里调用邮件程序。您可以在下面看到一个示例。

    app/mailers/user_mailer.rb

    class UserMailer < ActionMailer::Base
      default from: "something@something.com"
    
      def welcome_mail(email)
        mail(:to => email, :subject => "Welcome to Something").deliver
      end
    end
    

    app/models/user_observer.rb

    class UserObserver < ActiveRecord::Observer
      # We check if it's a new user
      def before_save(user)
        @is_new_record = user.new_record?
        true
      end
    
      def after_save(user)
        # If it's not a new user we don't want to send them another welcome email
        if @is_new_record then
          UserMailer.welcome_mail(user.email)
        end
      end
    end
    

    最后你需要配置rails来注册观察者。

    config/application.rb(只是摘录)

    config.active_record.observers = :user_observer
    

    【讨论】:

      【解决方案2】:

      回答可能太晚了,但我认为有 after_create 回调来缩小上面的解决方案,因为您不需要检查它是否是新记录!

      【讨论】:

        猜你喜欢
        • 2021-09-23
        • 2019-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多