1

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!

Albert Jin
  • 51
  • 1
  • 5

2 Answers2

0

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.

Ravi Prakash
  • 123
  • 1
  • 7
  • I'm sorry, I'm not too experienced with rails. How will the accept function get called? Can I make it so that when a form is submitted the function is called? – Albert Jin Mar 16 '18 at 08:46
  • yes, It will be called when on the active admin you will click on the accept link – Ravi Prakash Mar 17 '18 at 15:05
0

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

Piers C
  • 2,880
  • 1
  • 23
  • 29
  • Hi thanks for the response! Doesn't member action create a new url with in this case with "acceptreservation" at the end? How do I make it so this request is called when someone submits the active admin form? – Albert Jin Mar 16 '18 at 08:45
  • Use action_item to add a button/link to the member_action. – Piers C Mar 16 '18 at 15:33
  • Hi, sorry, quick question, what does accept_reservation_customer_path(customer) do? – Albert Jin Mar 29 '18 at 21:20
  • accept_reservation_customer_path is an auto-generated URL helper that will return something like "/admin/customers/1/accept_reservation" – Piers C Mar 30 '18 at 14:42
  • Oh, but its not necessary for functionality right? Because I get an error that customer is undefined. – Albert Jin Apr 01 '18 at 01:24
  • Should work, assuming the resource is Customer. Try `resource` instead. `resource` is a helper equivalent to `@customer ||= Customer.find params[:id]` – Piers C Apr 01 '18 at 13:44
  • Hi, thank you so much for helping. Sorry, what do you mean by the resources is Customer? Do you mean the person that I am emailing? That is a user object. Also, is the code under member action necessary? If i just have an action item that creates the button, and an empty member action, along with a new controller function that has the email logic, would that work? Thank you so much! – Albert Jin Apr 01 '18 at 21:04
  • Yes, if the resource is User, not Customer, then `resource` should still work. – Piers C Apr 02 '18 at 13:44