Sendgrid for ActiveAdmin

208 Views Asked by At

I have a website that currently sends an email to users once they sign up using sendgrid. This works all and well, but I cannot figure out how to do this:
When to user makes a reservation on my website(its kind of like open table), the reservation object is created. I then can go on Active Admin and change the status of the reservation to accepted. When I accept it, I want it to automatically send an email to the user. Does anybody know how I can do this? I've searched through the documentation and I cannot find anything specifically pertaining to Active Admin. I was thinking of it taking an action when the form is submitted aka the reservation status is changed by using sendgrid ruby (https://github.com/sendgrid/sendgrid-ruby). However, I have no idea how to do this. Can someone help me with this?

Thank You!

2

There are 2 best solutions below

2
On

There wont't be any predefined library for this. You have to call that method which sends the mail after you accept the reservation. under admin/registration.rb, do this:

controller do
  def accept
  # your accept logic
  end

  def send_mail
  # mail sending logic.
  end
end

You have to call the send_mail method after your accept logic or you can use callbacks to invoke send_mail after you accept this registration.

8
On

ActiveAdmin builds on Rails, so you can use ActionMailer, eg.

action_item :accept_reservation do
  link_to 'Accept reservation', accept_reservation_customer_path(resource)
end

member_action :accept_reservation do
  UserMailer.reservation_accepted(resource).deliver_now
  resource.update_attributes!(accepted_at: Time.now)
  redirect_to( {action: :show}, {notice: "Customer email sent."} )
end

ActionMailer works with various mail delivery services, SendGrid has some explanatory documentation