Tengo una gema devise_invitable
y un controlador de invitaciones InvitationsController < Devise::InvitationsController
. Invito a mi usuario con este método User.invite!(user, inviter)
de la gema.
Ahora, me gustaría agregar un archivo adjunto en pdf a esta invitación, pero no puedo anular el método de correo para hacer cosas como esta: attachments['terms.pdf'] = File.read('/path/terms.pdf')
.
Mi correo electrónico de invitación funciona bien y me gustaría mantenerlo así pero con un archivo adjunto.
Aquí mis routes.rb
:
devise_for :users, controllers: { invitations: 'api/v1/users/invitations' }
¿Qué me estoy perdiendo?
Gracias 🙏
DeviseInvitable tiene una opción skip_invitation
muy útil. Con el siguiente método, puede usar fácilmente su propio correo y agregarle un archivo adjunto:
def invite_and_notify_user(email) user = User.invite!({ email: email }) do |u| u.skip_invitation = true end notify_by_email(user) end def notify_by_email(user) MyUserMailer.invited_user_email(user).deliver end
En la clase MyUserMailer:
def invited_user_email(user) @user = user attachments['terms.pdf'] = File.read('/path/terms.pdf') mail(to: user.email, from: 'contact@mail.com', subject: "Your subject") end
Puede anular el correo del dispositivo así...
class MyMailer < Devise::Mailer helper :application # gives access to all helpers defined within `application_helper`. include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views # If there is an object in your application that returns a contact email, you can use it as follows # Note that Devise passes a Devise::Mailer object to your proc, hence the parameter throwaway (*). default from: ->(*) { Class.instance.email_address } end
Luego puede en su config/initializers/devise.rb
, establecer config.mailer
en "MyMailer". Tenga en cuenta que esto anula el correo utilizado para todos los módulos de dispositivos.
Luego, en esa clase, agregue su archivo adjunto a invite_user_instructions...
def invited_user_instructions(*args) attachments['terms.pdf'] = File.read('/path/terms/pdf') super end
Estas son las instrucciones del dispositivo https://github.com/heartcombo/devise/wiki/How-To:-Use-custom-mailer