Search code examples
ruby-on-railsdevisedevise-invitable

Devise invitable: Resend custom invitation with token


I am using a third party email service to handle all my emails and I am running into an issue when attempting to resend an invitation to a client.

When I attempt to perform the following, there is no raw_invitation_token available:

  def resendInvitation
    user = User.find(params[:id])
    user = User.invite!(email: user.email, location_id: user.location_id) do |u|
      u.skip_invitation = true
    end
    p "TOKEN: #{user.raw_invitation_token}"  # TOKEN IS NIL
    SendNewClientEmail.perform_later(user.id, user.raw_invitation_token)
  end

Here's the job that handles the invite:

class SendNewClientEmail < ApplicationJob
  queue_as :default

  def perform(user_id, token)
    @user = User.find(user_id)

    return unless @user.present? && @user.client.present?

    client = @user.client
    location = @user.location
    email = @user.email

    MailjetService.new(TEMPLATE_ID).send_email(
      location.email,
      email,
      'Welcome!',
      {
        'client_id' => client.try(:id),
        'token' => token
      },
      nil,
      "#{location.name.titleize}"
    )
  end
end

How else can I access that token so that the link in the email will work? Is there another token I can use or some other way this will work?


Solution

  • It is because the user is already created. Based on the Document Reference

    Please try the following

    user = User.find(:id)

    user.invite!({ skip_invitation: true }, current_user) # current user is optional to set the invited_by attribute

    You can also do this if you have added :skip_invitation to attributes hash if skip_invitation is added to attr_accessible.

    User.invite!(skip_invitation: true)

    When skip_invitation is used, you must also set the invitation_sent_at field when the user is sent their token. Failure to do so will yield an “Invalid invitation token” error when the user attempts to accept the invite.