Send Email with Ruby

Asked

Viewed 138 times

0

Good night! The scenario I find myself in is this::

I have a canvas that presents an already completed form. At the bottom of the screen, there is a button to send the link from form by email. And that’s where the magic happens:

Utilizo AJAX via Javascript:

$('.send_email').click(function () {
    const url = (window.location.href).split("/");
    $.ajax({
        url: " /assessments/"+url[url.length - 2]+"/send_email",
        type: "POST",
        data: {"assessments_id": url[url.length - 2]},
        dataType: "json",
        success: function() {
            alert('Enviado...');
        }
    });
});

It is working very well. Picking up the id contained in the URL and going on to request the POST.

In the method send_email, is where it gets interesting: He’s in a controller exactly that way:

  def send_email
    AssessmentMailer.assessment_report set_assessment
  end

The set_assessment method is this:

private
# Use callbacks to share common setup or constraints between actions.
def set_assessment
  @assessment = Assessment.find(params[:id])
end

And it’s working fine.

What I need is to send the email through send_email. But every time I try he returns with the Status 204

When debugging the system does not go to the class/method that is requested.

Follow The Mailer

class AssessmentMailer < ApplicationMailer
  default from: "[email protected]"
  before_action :load_assessments

  # Subject can be set in your I18n file at config/locales/en.yml
  # with the following lookup:
  #
  #   en.assessment_mailer.assessment_report.subject
  #
  def assessment_report
    mail to: @assessment.collaborator.email , subject: 'QUALIDADE DE VIDA'
  end

  private
  def load_assessments
    @assessment = params[:assessment]
  end
end

Segue Model:

class Assessment < ApplicationRecord
  belongs_to :collaborator
  paginates_per 10

  after_create :send_report_email

  def send_report_email
    AssessmentMailer.with(assessment: self).assessment_report.deliver_now!
  end
end

Who can save my existence Nothing but eternal gratitude! hahaha. Jokes aside...

  • HTTP 204 is successful. But no response content.

  • Yes, during the debugs I put in the model an "after_find" and passed the method send_report_email it works, only it sends an email for each time it does the find

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.