0

当我尝试将邮件发送到delayed_job 时,我收到“错误数量的参数(2 对1)”错误。如果我不尝试将其推到后台,一切都会完美。下面是我的带延迟作业的控制器:

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

当我ExportLogsMailer.email_logs(@user, @logs['sending_to']).deliver的邮件工作正常。我正在使用gem 'delayed_job_active_record'and gem 'rails', '3.2.13'

下面是 ExportLogsMailer 的样子:

class ExportLogsMailer < ActionMailer::Base
default :from => "support@myconsultantapp.com"


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

结尾

任何帮助将不胜感激。谢谢!

4

1 回答 1

1

您是否尝试仅检索一个用户?然后做:

@user = User.find(params[:user_id])

否则,如果您尝试将对象数组传递给delayed_job,我认为您需要all在最后添加:

@objects = Model.where(...).all
于 2013-04-23T15:30:56.310 回答