Rails 4.2 Active Job Resque Enqueue未定义方法问题
Rails 4.2 Active Job Resque Enqueue Undefined Method Issue
我正在关注 https://blog.engineyard.com/2014/getting-started-with-active-job 在 Rails 4.2 中实施 Active Job。但在 Resque 中面临以下问题。错误读取:
Worker
ChickenSmitten.local:64238 on EMAIL at just now Retry or Remove
Class
ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper
Arguments
---
job_class: PasswordResetJob
job_id: 35f70043-9a9b-4add-8700-a5f388350fb4
queue_name: email
arguments:
- bob@example.com
Exception
NoMethodError
Error
undefined method `email' for "bob@example.com":String
我的代码如下:
password_resets_controller.rb
def create
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:notice] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:error] = "Email address not found"
render 'new'
end
end
user.rb
def send_password_reset_email
PasswordResetJob.new(self.email).enqueue(wait: 10.seconds)
end
app/jobs/password_reset_job.rb
class PasswordResetJob < ActiveJob::Base
queue_as :email
def perform(email)
UserMailer.password_reset(email).deliver_now
end
end
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "noreply@example.com"
def password_reset(user)
@user = user
mail to: user.email, subject: "Password Reset"
end
end
不知何故,user.rb 中的“self.email”未传递给 password_reset_job.rb。因此,错误““bob@example.com”的“未定义方法‘email’:字符串”。
提前致谢。
更新
根据 Prakash 的建议并进行如下更改,将“self.email”更改为“self”:
def send_password_reset_email
PasswordResetJob.new(self).enqueue(wait: 10.seconds)
end
弹出一个新错误。这是错误:
ActionView::Template::Error
No route matches {:action=>"edit", :controller=>"password_resets", :email=>"bob@example.com", :format=>nil, :id=>nil} missing required keys: [:id]
以下是可能有所帮助的更多详细信息。
[2] pry(#<User>)> PasswordResetJob.new(self).enqueue(wait: 10.seconds)
[ActiveJob] Enqueued PasswordResetJob (Job ID: 6bdcca3c-367d-48ea-b3c9-1f5378e5df57) to Resque(email) at 2015-01-06 04:58:28 UTC with arguments: gid://affiliation/User/1
=> #<PasswordResetJob:0x007fbd4c028dc8
@arguments=
[#<User:0x007fbd4b686590
id: 1,
username: "bob",
email: "bob@example.com",
password_digest: "a$dbbEBwNgBtaZEvtl7EsUm./hdukr3MMKlUWdouV3RwIFMmMPfR6CS",
created_at: Thu, 01 Jan 2015 03:38:54 UTC +00:00,
updated_at: Tue, 06 Jan 2015 04:58:01 UTC +00:00,
role: "admin",
slug: "bob",
activation_digest: "a$wdoO7vn5Ng4U4TEPCmqVFeVYAB8sZ2CKEpVFfduYyD7Ee.NfvSRsi",
activated: true,
activated_at: Thu, 01 Jan 2015 03:41:19 UTC +00:00,
remember_digest: nil,
reset_digest: "a$Eu1jYllohAY2IQO4Uc.0TuyNmRWWuu3jQgjZrIZLwFlo1t8n1hNZO",
reset_sent_at: Tue, 06 Jan 2015 04:58:01 UTC +00:00,
url: "www.google.com",
bio: "Woo ga shaka.">],
@job_id="6bdcca3c-367d-48ea-b3c9-1f5378e5df57",
@queue_name="email",
@scheduled_at=1420520308.6658938>
views/user_mailer/password_reset.html.rb
<h1>Password reset</h1>
<p>To reset your password click the link below:</p>
<%= link_to "Reset password", edit_password_reset_url(@user.reset_token,
email: @user.email) %>
<p>This link will expire in two hours.</p>
<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>
def perform(email)
UserMailer.password_reset(email).deliver_now
end
这是在 UserMailer
中使用 email
调用 password_reset
方法(在此特定情况下其值为 bob@example.com
。
def password_reset(user)
@user = user
mail to: user.email, subject: "Password Reset"
end
所以在 password_reset
方法中,user
被设置为 bob@example.com
,因此在调用 user.email
时失败。
解决此问题的一种方法是按如下方式制作方法:
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "noreply@example.com"
def password_reset(email)
mail to: email, subject: "Password Reset"
end
end
您可能需要根据具体需要寻找不同的方式。
您正在将电子邮件传递给 PasswordResetJob,后者将其传递给邮件程序,但邮件程序需要一个 User 实例。
PS:可以直接在模型中做UserMailer.password_reset(self.email).deliver_later。参见 http://api.rubyonrails.org/classes/ActionMailer/MessageDelivery.html#method-i-deliver_later
我正在关注 https://blog.engineyard.com/2014/getting-started-with-active-job 在 Rails 4.2 中实施 Active Job。但在 Resque 中面临以下问题。错误读取:
Worker
ChickenSmitten.local:64238 on EMAIL at just now Retry or Remove
Class
ActiveJob::QueueAdapters::ResqueAdapter::JobWrapper
Arguments
---
job_class: PasswordResetJob
job_id: 35f70043-9a9b-4add-8700-a5f388350fb4
queue_name: email
arguments:
- bob@example.com
Exception
NoMethodError
Error
undefined method `email' for "bob@example.com":String
我的代码如下:
password_resets_controller.rb
def create
@user = User.find_by(email: params[:password_reset][:email].downcase)
if @user
@user.create_reset_digest
@user.send_password_reset_email
flash[:notice] = "Email sent with password reset instructions"
redirect_to root_url
else
flash.now[:error] = "Email address not found"
render 'new'
end
end
user.rb
def send_password_reset_email
PasswordResetJob.new(self.email).enqueue(wait: 10.seconds)
end
app/jobs/password_reset_job.rb
class PasswordResetJob < ActiveJob::Base
queue_as :email
def perform(email)
UserMailer.password_reset(email).deliver_now
end
end
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "noreply@example.com"
def password_reset(user)
@user = user
mail to: user.email, subject: "Password Reset"
end
end
不知何故,user.rb 中的“self.email”未传递给 password_reset_job.rb。因此,错误““bob@example.com”的“未定义方法‘email’:字符串”。
提前致谢。
更新
根据 Prakash 的建议并进行如下更改,将“self.email”更改为“self”:
def send_password_reset_email
PasswordResetJob.new(self).enqueue(wait: 10.seconds)
end
弹出一个新错误。这是错误:
ActionView::Template::Error
No route matches {:action=>"edit", :controller=>"password_resets", :email=>"bob@example.com", :format=>nil, :id=>nil} missing required keys: [:id]
以下是可能有所帮助的更多详细信息。
[2] pry(#<User>)> PasswordResetJob.new(self).enqueue(wait: 10.seconds)
[ActiveJob] Enqueued PasswordResetJob (Job ID: 6bdcca3c-367d-48ea-b3c9-1f5378e5df57) to Resque(email) at 2015-01-06 04:58:28 UTC with arguments: gid://affiliation/User/1
=> #<PasswordResetJob:0x007fbd4c028dc8
@arguments=
[#<User:0x007fbd4b686590
id: 1,
username: "bob",
email: "bob@example.com",
password_digest: "a$dbbEBwNgBtaZEvtl7EsUm./hdukr3MMKlUWdouV3RwIFMmMPfR6CS",
created_at: Thu, 01 Jan 2015 03:38:54 UTC +00:00,
updated_at: Tue, 06 Jan 2015 04:58:01 UTC +00:00,
role: "admin",
slug: "bob",
activation_digest: "a$wdoO7vn5Ng4U4TEPCmqVFeVYAB8sZ2CKEpVFfduYyD7Ee.NfvSRsi",
activated: true,
activated_at: Thu, 01 Jan 2015 03:41:19 UTC +00:00,
remember_digest: nil,
reset_digest: "a$Eu1jYllohAY2IQO4Uc.0TuyNmRWWuu3jQgjZrIZLwFlo1t8n1hNZO",
reset_sent_at: Tue, 06 Jan 2015 04:58:01 UTC +00:00,
url: "www.google.com",
bio: "Woo ga shaka.">],
@job_id="6bdcca3c-367d-48ea-b3c9-1f5378e5df57",
@queue_name="email",
@scheduled_at=1420520308.6658938>
views/user_mailer/password_reset.html.rb
<h1>Password reset</h1>
<p>To reset your password click the link below:</p>
<%= link_to "Reset password", edit_password_reset_url(@user.reset_token,
email: @user.email) %>
<p>This link will expire in two hours.</p>
<p>
If you did not request your password to be reset, please ignore this email and
your password will stay as it is.
</p>
def perform(email)
UserMailer.password_reset(email).deliver_now
end
这是在 UserMailer
中使用 email
调用 password_reset
方法(在此特定情况下其值为 bob@example.com
。
def password_reset(user)
@user = user
mail to: user.email, subject: "Password Reset"
end
所以在 password_reset
方法中,user
被设置为 bob@example.com
,因此在调用 user.email
时失败。
解决此问题的一种方法是按如下方式制作方法:
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "noreply@example.com"
def password_reset(email)
mail to: email, subject: "Password Reset"
end
end
您可能需要根据具体需要寻找不同的方式。
您正在将电子邮件传递给 PasswordResetJob,后者将其传递给邮件程序,但邮件程序需要一个 User 实例。
PS:可以直接在模型中做UserMailer.password_reset(self.email).deliver_later。参见 http://api.rubyonrails.org/classes/ActionMailer/MessageDelivery.html#method-i-deliver_later