Rails与邮件程序的delayed_job参数错误

时间:2013-04-23 14:53:11

标签: ruby-on-rails heroku actionmailer delayed-job

当我尝试将邮件发送到delayed_job时,我收到了“错误的参数数量(2 for 1)”错误。如果我不尝试将它推到后台,一切都很完美。下面是我的带有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

当我ExportLogsMailer.email_logs(@user, @logs['sending_to']).deliver邮件工作正常时。我正在使用gem 'delayed_job_active_record'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

非常感谢任何帮助。谢谢你!

1 个答案:

答案 0 :(得分:1)

您是否只想检索一个用户?然后做:

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

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

@objects = Model.where(...).all