Search code examples
devisedelayed-jobmandrilldevise-async

Devise async with Mandrill mailer wrong number of arguments (1 for 2)


I'm using delayed_job to handle background jobs. I recently came across the devse_async gem to integrated delayed_job and devise.

I am receiving a wrong number of arguments (1 for 2) error when delayed_job processes the email job in the queue. I should note I'm using Mandrill's API to send these emails.

delayed job console

Starting job worker
[Worker(host:MacBook-Pro.local pid:21007)] Job Devise::Async::Backend::DelayedJob#perform (id=1) RUNNING
[Worker(host:MacBook-Pro.local pid:21007)] Job Devise::Async::Backend::DelayedJob#perform (id=1) FAILED (0 prior attempts) with ArgumentError: wrong number of arguments (1 for 2)
[Worker(host:MacBook-Pro.local pid:21007)] 1 jobs processed at 30.3564 j/s, 1 failed

trace

wrong number of arguments (1 for 2)
/Users/Sites/app/mailers/devise_mailer.rb:6:in `confirmation_instructions'

devise_mailer

class DeviseMailer < MandrillMailer::TemplateMailer
  include Devise::Controllers::UrlHelpers
  include Devise::Mailers::Helpers
  default from: 'no-reply@foobar.com'

  def confirmation_instructions(record, token)
    @resource = record

    # Route mailer to send the proper subject and template
    if @resource.pending_reconfirmation?
      confirmation_template = 'Reconfirmation Instructions'
      confirmation_subject = 'Confirm your new email'
    else
      confirmation_template = 'Confirmation Instructions'
      confirmation_subject = 'Confirmation Email'
    end

    # Include proper greeting in email based on User type
    recipient_name = nil
    if @resource.type == "Business"
      recipient_name = record.business_name
    else
      recipient_name = record.first_name
    end
    puts "sending confirmation email"
    host =   ActionMailer::Base.default_url_options[:host]
    mandrill_mail template: confirmation_template,
              subject:  confirmation_subject,
              from_name: 'foobar',
              to: { email: 'contact@foobar.ca' },
              vars: {
                'FNAME'                  => recipient_name,
                'LIST_COMPANY'           => "foobar",
                'HTML_LIST_ADDRESS_HTML' => "foobar",
                'CONFIRMATION_LINK'      => "%s/users/confirmation?confirmation_token=#{record.confirmation_token}" % ENV['MAIL_HOST']
                # "http://0.0.0.0:3000/users/confirmation?confirmation_token=#{record.confirmation_token}"
              },
              async: true   
  end
end

registrations_controller.rb

def new
  build_resource({})
  resource.build_supp_form
  respond_with self.resource
end

def create
  super
end

Solution

  • The use of database encrypted tokens was introduced in devise 3.1 (https://github.com/plataformatec/devise/blob/master/CHANGELOG.md#310---2013-09-05), so your confirmation_instructions mailer method doesn't expect any token as the second parameter.

    In fact you're not using that parameter anywhere in your method, notice you call record.confirmation_token.

    Just remove the second parameter in the method signature and you should be good to go:

    def confirmation_instructions(record)
      ...
    end