Ich versuche herauszufinden, wie eine Mailer-Klasse in meiner Rails 4 App eingerichtet wird.Rails 4 - mailer - wie Argumente an eine Mailer-Methode übergeben werden
Ich habe einen Mailer namens admin_notes gemacht. Ich möchte damit E-Mails an das interne Team senden, wenn bestimmte Aktionen auf der Site ausgeführt werden.
In meinem Mailer/admin_note.rb, ich habe:
class AdminNote < ApplicationMailer
def unknown_organisation(organisation_request, user_full_name, name)
@organisation_request =
@user_full_name =
@organisation_request.name =
# @greeting = "Hi"
mail(to: "[email protected]",from: "[email protected]", subject: "A new organisation")
end
end
ich ein organisation_requests Modell. Es hat:
class OrganisationRequest < ActiveRecord::Base
belongs_to :profile
delegate :user_full_name, to: :profile, prefix: false, allow_nil: true
Die Organisationsanforderungstabelle hat ein Attribut namens: name drin.
Wenn eine neue Organisationsanforderung erstellt wird, möchte ich eine Admin-Notiz an das interne Team senden und jemanden darauf hinweisen, einen Prozess zu starten.
Ich habe Mühe herauszufinden, wie ich die drei Variablen in der Mailer-Methode definieren.
Ich plane, den E-Mail-Sendeaufruf zur Erstellungsaktion im Organisationsanfragen-Controller hinzuzufügen.
Wie kann ich diese Variablen setzen?
Formular eine Organisation Anfrage zu erstellen ist:
<%= simple_form_for(@organisation_request) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :organisation_id, collection: @all_organisations << ['other', nil] %>
</div>
<div class="form-inputs">
<%= f.input :name %>
</div>
<div class="form-actions">
<%= f.button :submit, "Create", :class => 'formsubmit' %>
</div>
<% end %>
NEW VERSUCH:
Ich habe eine Aktion erstellen in meiner Organisation Controller, ich diese Serviceklasse Anforderung für eine E-Mail hinzugefügt:
def create
@organisation_request = OrganisationRequest.new(organisation_request_params)
@organisation_request.profile_id = current_user.profile.id
if @organisation_request.save
NewOrgRequestService.send_unknown_organisation_requested_flag(organisation_request)
return redirect_to(profile_path(current_user.profile),
flash[:alert] => 'Your request is being processed.')
else
# Failure scenario below
@all_organisations = Organisation.select(:title, :id).map { |org| [org.title, org.id] }
render :new
end
end
Ich habe dann eine Dienstleistungen/Organisationen Anfragen/NewOrgRequestService.rb
class OrganisationRequest < ActiveRecord::Base
class NewOrgRequestService
attr_accessor :organisation_request
def self.send_unknown_organisation_requested_flag(organisation_request)
if @organisation_request.name.present?
AdminNote.unknown_organisation_requested(organisation_request, user_full_name, name).deliver_later
end
end
end
end
Der AdminNote Mailer hat:
class AdminNote < ApplicationMailer
layout 'transactional_mailer'
def unknown_organisation_requested(organisation_request, user_full_name, name)
@organisation_request = @organisation_request
@user_full_name = @organisation_request.user_full_name
@name = organisation_request.name
# @greeting = "Hi"
mail
to: "[email protected]",from: "[email protected]", subject: "A new organisation"
end
end
diese nicht funktioniert, aber ich frage mich, wenn Im auf dem richtigen Weg? Ich bin nicht sicher, ob die Erstellungsaktion im Controller irgendeine Art von Verweis auf die Dienste/organisation_requests/path haben muss, die in die Datei gelangen?
Ich denke, dass ich eine größere Sauerei gemacht habe, als ich anfing - aber ich habe keine Ideen für Dinge, die ich als nächstes versuchen sollte.
warum Sie delegieren verwenden brauchen ... ich kann – Milind
ich die Delegation für mehrere Access Points verwenden, ohne ein Delegat-Modul durchgeführt werden. es ist nützlich für mich – Mel