Come up with an optional optional custom message

I would like to add an optional text field to add an optional message to the invitation email address. Therefore, if nothing is placed in the message, then the standard message from the prompt_instruction .html.erb should be sent only as a text field message.

How to do this in development?

+4
source share
1 answer

To do this, you can use your own email program instead of creating email for you.

Here's how to do it:

class User < ActiveRecord::Base attr_reader :raw_invitation_token end class InvitationsController < Devise::InvitationsController def create @from = params[:from] @subject = params[:invite_subject] @content = params[:invite_content] @user = User.invite!(params[:user], current_user) do |u| u.skip_invitation = true end @user.deliver_invitation email = NotificationMailer.invite_message(@user, @from, @subject, @content) end end class NotificationMailer < ActionMailer::Base def invite_message(user, venue, from, subject, content) @user = user @token = user.raw_invitation_token invitation_link = accept_user_invitation_url(:invitation_token => @token) mail(:from => from, :bcc => from, :to => @user.email, :subject => subject) do |format| content = content.gsub '{{first_name}}', user.first_name content = content.gsub '{{last_name}}', user.first_name content = content.gsub '{{full_name}}', user.full_name content = content.gsub('{{invitation_link}}', invitation_link) format.text do render :text => content end end end end 

raw_invitation_token exists only in newer versions of devise_invitable (compatible with devise> = 3.1).

We essentially skip the process of inviting developers together and using our own email program. We take the content as a parameter and send it as the body of the message. We even replace placeholders with our actual values.

This solution will give you great flexibility for everyone you want via email. You can even add a tracking pixel if you want.

+5
source

Source: https://habr.com/ru/post/1403104/


All Articles