0

I'm trying to create an admin Tool where the admins can send emails that are already there with a button.

I have a class FamilyMailer < ActionMailer::Base with this method birthdaymailer

Is there a way for me to trigger this from the view ?

I have seen a lot of answers on StackOverFlow, but it doesn't really work.

The closest conclusion I have came across is that the Mailer gets triggered this way FamilyMailer.birthdaymailer in the view and create a return to main page.

I was wondering if there would be a better way.

0bserver07
  • 3,390
  • 1
  • 28
  • 56

1 Answers1

2

Sure!

Say you've got a mailer, called UserMailer.

Set up your controller action:

# my_controller.rb

def send_mail
  @parameters = Model.get_parameters
  UserMailer.name_of_action(@parameters).deliver
end

Set up your route:

get send_mail, to: 'my_controller#send_mail', as: :send_mail

Then make your link!

link_to 'Send mail', send_mail_path, class: "big-button"

Voila.

steel
  • 11,883
  • 7
  • 72
  • 109
  • 1
    if you want this to be semi silent, you can (following the above) call the endpoint with AJAX or add a redirect at the end of the method to put you back on the page you started on. – mr rogers Jun 06 '14 at 03:33
  • I ended up doing something similar :)! BUT I will still choose it as an answer, because I still didn't know this would be possible! Thanks @Steel – 0bserver07 Jun 06 '14 at 04:58