Ruby/ Rails: ActiveJob 的未定义方法“set”
Ruby/ Rails: undefined method `set' for 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
我的控制器:
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
Rails 4.1.8
在你的控制器中试试这个:
SendEmailJob.new(@user).enqueue(wait: 20.seconds)
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
之间的活跃工作差异
我创建了一个这样的工作:
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
Rails 4.1.8
在你的控制器中试试这个:
SendEmailJob.new(@user).enqueue(wait: 20.seconds)
ActiveJob 已与版本 4.2
在此之前,您需要使用 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
之间的活跃工作差异