Good evening! The scenario I encounter is as follows:
I have a screen that shows a form already filled out. At the bottom of the screen, there is a button to send the link from form via email. And that's where magic happens:
I use 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. Taking the id contained in the URL and passing to request the POST.
In the send_email
method, this is where it gets interesting:
It's in a controller exactly like this:
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 whenever I try it returns with 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
Follows 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! haha ha. Jokes aside ...