2013-04-23 83 views
0

當我嘗試將郵件發送到delayed_job時,出現「錯誤數量的參數(2代表1)」錯誤。如果我不試圖將其推到背景,那麼一切都很完美。下面是我的控制器的delayed_job:Rails的郵件與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 => "[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 

任何幫助將不勝感激。謝謝!

+0

什麼ExportLogsMailer看起來不一樣? – Ari 2013-04-23 15:16:30

+0

我剛剛添加了ExportLogsMailer的代碼 – KevinM 2013-04-23 15:25:02

+0

請將'export_logs'方法添加到您的問題中。 – 2013-04-23 15:32:08

回答

1

你想要檢索一個用戶嗎?然後做:

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

否則,如果你想傳遞對象數組來delayed_job的,我認爲你需要在末尾添加all

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

這是問題所在。我改變了user = User.where(「id =?」,params [:user_id])到user = User.where(「id =?」,params [:user_id])。謝謝! – KevinM 2013-04-23 16:13:02

相關問題