【问题标题】:Rails delayed_job argument error with mailer邮件程序的 Rails delay_job 参数错误
【发布时间】:2013-04-23 14:53:11
【问题描述】:

当我尝试将邮件发送到 delay_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'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

结束

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

【问题讨论】:

  • ExportLogsMailer 是什么样的?
  • 我刚刚添加了 ExportLogsMailer 的代码
  • 请将export_logs 方法添加到您的问题中。

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


【解决方案1】:

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

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

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

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

【讨论】:

  • 这就是问题所在。我将 user = User.where("id = ?", params[:user_id]) 更改为 user = User.where("id = ?", params[:user_id]).all 现在可以使用了。谢谢!
猜你喜欢
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 2012-01-21
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 2015-04-22
  • 1970-01-01
相关资源
最近更新 更多