Reputation: 1807
I'm getting a "wrong number of arguments (2 for 1)" error when I try send a mailer to delayed_job. Everything works perfect if I don't try to push it to the background. Below is my controller with delayed_job:
def export
@user = User.where("id = ?", params[:user_id])
@logs = VehicleMileage.export_logs(params, @user)
if @logs['log_count'] > 0
ExportLogsMailer.delay.email_logs(@user, @logs['sending_to'])
end
respond_to do |format|
formats # index.html.erb
format.json { render json: @logs }
end
end
When I ExportLogsMailer.email_logs(@user, @logs['sending_to']).deliver
the mailer works fine. I'm using the gem 'delayed_job_active_record'
and gem 'rails', '3.2.13'
.
Here is what ExportLogsMailer looks like:
class ExportLogsMailer < ActionMailer::Base
default :from => "[email protected]"
def email_logs(user, emails)
# make sure emails are unique
emails.uniq
first_name = user[0].first_name
last_name = user[0].last_name
# encoded_content = Base64.strict_encode64(File.read("#{Rails.root}/tmp/test.xls"))
# puts encoded_content
# attachments['test.xls'] = { :content => encoded_content,
# :encoding => 'Base64'
# }
# Name of template in your Mandrill template area
headers['X-MC-Template'] = 'Export Logs'
# Tags help classify your messages
headers['X-MC-Tags'] = 'mileage logs'
# Enable open or click-tracking for the message.
# Only can track to at a time. Possibilies: opens, clicks, clicks_htmlonly, clicks_textonly
headers['X-MC-Track'] = 'opens, clicks'
# Automatically generate a plain-text version of the email from the HTML content.
headers['X-MC-Autotext'] = 'true'
# Add dynamic data to replace mergetags that appear in your message content. Should be a JSON-formatted
# object and flat, so if you more than one recipient then add another X-MC-MergeVars to the header. The
# below example will change anywhere where *|FNAME|* or *|LNAME|* to the respective value.
mergeVars =
{
"fname" => first_name,
"lname" => last_name
}
headers['X-MC-MergeVars'] = mergeVars.to_json
# Add Google Analytics tracking to links in your email for the specified domains.
headers['X-MC-GoogleAnalytics'] = 'http://www.myconsultantapp.com/'
# Add an optional value to be used for the utm_campaign parameter in Google Analytics tracked links.
# headers['X-MC-GoogleAnalyticsCampaign'] = ''
# Information about any custom fields or data you want to append to the message.
# Up to 200 bytes of JSON-encoded data as an object. The object should be flat; nested object structures are not supported.
# headers['X-MC-Metadata'] = ''
# Whether to strip querystrings from links for reporting. "true" or "false"
# headers['X-MC-URLStripQS'] = 'true'
# Whether to show recipients of the email other recipients, such as those in the "cc" field. "true" or "false"
headers['X-MC-PreserveRecipients'] = 'false'
message = prepare_message :subject => "1 myConsultant logs",
:to => emails,
:content_type => "multipart/mixed"
message.alternative_content_types_with_attachment(
:text => 'text',
:html => 'html'
) do |i|
i.inline['test.xls'] = File.read("#{Rails.root}/tmp/test.xls")
end
message
end
end
Any help would be greatly appreciated. Thanks you!
Upvotes: 0
Views: 635
Reputation: 4880
Are you trying to retrieve just one user? Then do:
@user = User.find(params[:user_id])
Otherwise, if you're trying to pass an array of objects to delayed_job, I think you need to add all
at the end:
@objects = Model.where(...).all
Upvotes: 1