【问题标题】:Ruby/ Rails: undefined method `set' for ActiveJobRuby/Rails:ActiveJob 的未定义方法“set”
【发布时间】:2015-08-26 01:19:02
【问题描述】:

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

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 的未定义方法“设置”):

编辑配置/应用程序.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

【问题讨论】:

  • 你能显示你的 config/application.rb` 文件吗?另外,你的 Rails 版本是什么?
  • @ojdq 的答案应该有效,engineyard blog.engineyard.com/2014/getting-started-with-active-job 中有一篇关于如何做同样事情的博客文章。但是,如果可能的话,我更感兴趣的是查看您的堆栈跟踪(以便我可以尝试跟踪哪个文件给出了这个错误)

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


【解决方案1】:

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 之间的当前工作差异

【讨论】:

  • 我不得不使用 SendEmailJob.enqueue(@user)
  • 正确。当您使用 Rails 4.1.8 时,这是正确的语法。第二个参数是一个选项哈希。如果你不需要它,你不发送它。
  • @josh 如果这有助于您解决问题,请考虑支持/接受答案。谢谢。
  • 查看接受答案的工作原理:meta.stackexchange.com/questions/5234/…
【解决方案2】:

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

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

【讨论】:

  • 我是否需要将 send_email_job.rb 中的 def perform(user) 操作更改为 new(user)?获取 ArgumentError(参数数量错误(1 代表 0)。
  • 在我的情况下,我这样做: class SendEmailJob
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-25
  • 2010-10-15
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多