Rails - Devise send user email after sign_up/create

后端 未结 2 1658
无人及你
无人及你 2020-12-23 20:29

I\'m fairly new to rails and trying to figure things out. I recently got a mailer all setup and it was working fine. But I am trying to add a second mailer for user actions

相关标签:
2条回答
  • 2020-12-23 21:06

    When users sign up with Devise, they don't go through the UsersController.

    You might want to add the mail sending code in the User model.

    For example, in app/models/user.rb:

    class User < ActiveRecord::Base
      # ...
    
      after_create :send_admin_mail
      def send_admin_mail
        UserMailer.send_new_user_message(self).deliver
      end
    
      # ...
    end
    

    This is done by utilizing the Active Record after_create callback.

    0 讨论(0)
  • 2020-12-23 21:13

    Confirmation emails should be sent from the controller. It's simple to override the Devise::RegistrationsController defaults.

    Create the file app/controllers/my_registrations_controller.rb (name this whatever you want)

    class MyRegistrationsController < Devise::RegistrationsController
    
      def create
        super
        if @user.persisted?
          UserMailer.new_registration(@user).deliver
        end
      end
    
    end
    

    Then in your routes:

    devise_for :users, :controllers => { :registrations => "my_registrations" }
    
    0 讨论(0)
提交回复
热议问题