ruby-on-rails - ruby /Rails : undefined method `set' for ActiveJob

标签 ruby-on-rails ruby email ruby-on-rails-4 rails-activejob

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

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

我的 Controller :

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

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

编辑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

Rails 4.1.8

最佳答案

ActiveJob从4.2版本开始与Rails集成

在此之前,您需要使用 active_job gem。当您使用 Rails 版本 4.1.8 时,您必须使用 active_job gem 以及旧语法。 .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 中引入的

参见this article Rails 4.1 和 4.2 之间的事件作业差异

关于ruby-on-rails - ruby /Rails : undefined method `set' for ActiveJob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32216529/

相关文章:

ruby-on-rails - 如何用 has_many 保存数据 :through

ruby-on-rails - 直接链接(无重定向)到 ActiveStorage 中的文件

java - Spring 中的自定义电子邮件服务 - 需要帮助改进

javascript - 从数据库获取电子邮件值并发送(PHP电子邮件)/刷新页面而不更改网站链接

ruby-on-rails - 将博客/CMS 集成到 Heroku Rails 应用程序中

ruby-on-rails - 如何在带有自定义标签的表单中使用 Cucumber

Ruby SOCKsify 的 Python 等效项

ruby-on-rails - Rails 中的局部变量与实例变量?

php - 使用 php mail() 函数,建议在 BCC 中使用多少封电子邮件?

ruby-on-rails - 连续两行的最大列差