0

I want to send a email to user after create something via rails admin.

I know I can call it in model callback but it's not considered as a good pratices

the best way is to put the actionmailer action after model save in the controller but I don't know how to do it in rails_admin controller

class UsersController < ApplicationController
  # POST /users
  # POST /users.json
  def create
    @user = User.new(params[:user])

    respond_to do |format|
      if @user.save
        # Tell the UserMailer to send a welcome email after save
        UserMailer.welcome_email(@user).deliver_later

        format.html { redirect_to(@user, notice: 'User was successfully created.') }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: 'new' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
end
buncis
  • 2,148
  • 1
  • 23
  • 25

2 Answers2

0

What you want to do is not easy on rails admin because you cannot modify the controllers nor do you have access to them without monkey patching them.

I actually made a fork of rails admin for this functionality checkout the commit with the changes: https://github.com/aliada-mx/rails_admin/commit/6251554efd1d83cdb418f42683ee55a4e27c2474 Just touched two files

And example usage

class User
   after_save :on_admin_updates
   attr_accessor :edited_in_rails_admin

   def on_admin_updates
      return unless edited_in_rails_admin
      self.edited_in_rails_admin = false

     UserMailer.welcome_email(self.id)
  end
end

A bit clunky i know, PR´s welcome.

Guillermo Siliceo Trueba
  • 4,251
  • 5
  • 34
  • 47
-1

Have you tried to include your Mailer in admin_controller?

include UserMailer

Then in your create action UserMailer.some_mailer_action.deliver_now

youwan
  • 31
  • 4