2

我创建了一个这样的工作:

class SendEmailJob < ActiveJob::Base
  queue_as :default

   def perform(user)
    @user = user
    UserMailer.welcome_email(@user).deliver_later
   end

end

那使用我的邮件:

class UserMailer < ActionMailer::Base

  def welcome_email(user)
    @user = user
    mg_client = Mailgun::Client.new ENV['api_key']
    message_params = {
      :from   => ENV["gmail_username"],
      :to     => @user.email,
      :subject => "Welcome",
      :text =>    "This is a welcome email"
    }
    mg_client.send_message ENV["domain"], message_params
  end

end

我的控制器:

  SendEmailJob.set(wait: 20.seconds).perform_later(@user)

我不断收到以下错误:NoMethodError(SendEmailJob:Class 的未定义方法“设置”):

编辑 config/application.rb 需要 File.expand_path('../boot', FILE )

require 'rails/all'
require 'active_job'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module LinkbuilderPro
  class Application < Rails::Application
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.

    # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
    # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
    # config.time_zone = 'Central Time (US & Canada)'

    # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
    # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
    # config.i18n.default_locale = :de
  end
end

导轨 4.1.8

4

2 回答 2

1

在你的控制器中试试这个:

SendEmailJob.new(@user).enqueue(wait: 20.seconds)
于 2015-08-26T02:16:07.030 回答
1

ActiveJob 从版本中与 Rails 集成4.2

在此之前,您需要使用active_jobgem。当您使用 Rails 版本4.1.8时,您必须将active_jobgem 与旧语法一起使用。.set方法在 Rails 4.2 之前不可用,所以你得到了那个错误。

但是,Rails 4.1 版的语法是:

YourJob.enqueue(record)
YourJob.enqueue(record, options)

所以,在你的情况下,它会是这样的:

SendEmailJob.enqueue(@user, wait: 20.seconds)

perform_later在 Rails 4.2 中引入

请参阅本文了解 Rails 4.1 和 4.2 之间的活动工作差异

于 2015-08-26T03:04:33.870 回答