Ruby 在 Rails 上:Sidekiq:实例变量@organisation 在邮件布局中不可用
Ruby On Rails : Sidekiq : Instance variable @organisation not available in mailer-layout
我已经使用 sidekiq
和 redis-server
在后台发送电子邮件..
问题:用sync方式发送邮件没问题。 IE。
在 applicants_controller.rb
UserMailer.notify_applicant_assignment(current_assigned_user.id, applicant, workflow_step).deliver
但是,当我使用 delay
方法发送电子邮件时,即
在applicants_controller.rb
UserMailer.delay.notify_applicant_assignment(current_assigned_user.id, applicant, workflow_step)
我在 /layouts/user_mailer.html.erb:17:
中收到以下错误 undefined method 'background_color' for nil:NilClass
里面的代码mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
include Sidekiq::Worker
default from: CommonConstants::DO_NOT_REPLY_ADDRESS
layout 'user_mailer'
def notify_applicant_assignment(user_id, applicant_id, workflow_step_id)
@user = User.find(user_id)
@organization = @user.organization
@applicant = Applicant.find(applicant_id)
@url = root_url + 'applicants/' + @applicant.id.to_s
@workflow_step = WorkflowStep.find(workflow_step_id)
mail(to: @user.email, subject: 'Applicant Assigned.')
end
end
里面的代码layouts/user_mailer.html.erb
<body style="background:#f4f4f4;">
<table width="100%" bgcolor="<%= @organization.background_color %>" cellpadding="0" cellspacing="0">
<tr>
<td>
<table align="center" width="730" cellpadding="0" cellspacing="0" >
sidekiq 控制台出错
2015-03-24T08:58:14Z 5595 TID-cjors WARN: {"retry"=>true, "queue"=>"default", "class"=>"Sidekiq::Extensions::DelayedMailer", "args"=>["---\n- !ruby/class 'UserMailer'\n- :notify_applicant_assignment\n- - 4\n - '9'\n - '9'\n"], "jid"=>"4421abf04e7e6864c7ee9fd8", "enqueued_at"=>1427187124.323067, "error_message"=>"undefined method `background_color' for nil:NilClass", "error_class"=>"ActionView::Template::Error", "failed_at"=>1427187124.3466575, "retry_count"=>4, "retried_at"=>1427187494.9246943}
2015-03-24T08:58:14Z 5595 TID-cjors WARN: undefined method `background_color' for nil:NilClass
2015-03-24T08:58:14Z 5595 TID-cjors WARN: /home/leapfrog/projects/ATS/app/views/layouts/user_mailer.html.erb:17:in `_app_views_layouts_user_mailer_html_erb__235114594899105140_70586860'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/template.rb:143:in `block in render'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/activesupport-4.0.4/lib/active_support/notifications.rb:161:in `instrument'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/template.rb:141:in `render'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
/
延迟扩展的工作方式很可能与您预期的不同。它们与常规异步作业不同。
在the documentation中明确指出:
I strongly recommend avoiding delaying methods on instances. This stores object state in Redis which can get out of date, causing stale data problems.
如果延迟扩展存储未呈现的模板和邮件参数(主题、收件人、发件人等)而不是为过时的实例调用方法本身,我不会感到惊讶。
此外,请注意 notify_applicant_assignment
未定义为 class 方法(它看起来像实例方法),尽管 Sidekiq documentation 声明延迟应应用于 class 方法。
Any class method can be delayed via the same methods as above:
我建议将 notify_applicant_assignment
设为 class 方法并重试(注意 def self.notify_applicant_assignment
与 [=15 中的 self =]).
class UserMailer < ActionMailer::Base
include Sidekiq::Worker
default from: CommonConstants::DO_NOT_REPLY_ADDRESS
layout 'user_mailer'
def self.notify_applicant_assignment(user_id, applicant_id, workflow_step_id)
@user = User.find(user_id)
@organization = @user.organization
@applicant = Applicant.find(applicant_id)
@url = root_url + 'applicants/' + @applicant.id.to_s
@workflow_step = WorkflowStep.find(workflow_step_id)
mail(to: @user.email, subject: 'Applicant Assigned.')
end
end
或者,我还建议在 applicants_controller.rb
中使用标准异步引擎:
UserMailer.notify_applicant_assignment_async(current_assigned_user.id, applicant, workflow_step)
请告诉我进展如何,如果还有需要,我可以再研究一下。
祝你好运!
我已经使用 sidekiq
和 redis-server
在后台发送电子邮件..
问题:用sync方式发送邮件没问题。 IE。
在 applicants_controller.rb
UserMailer.notify_applicant_assignment(current_assigned_user.id, applicant, workflow_step).deliver
但是,当我使用 delay
方法发送电子邮件时,即
在applicants_controller.rb
UserMailer.delay.notify_applicant_assignment(current_assigned_user.id, applicant, workflow_step)
我在 /layouts/user_mailer.html.erb:17:
undefined method 'background_color' for nil:NilClass
里面的代码mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
include Sidekiq::Worker
default from: CommonConstants::DO_NOT_REPLY_ADDRESS
layout 'user_mailer'
def notify_applicant_assignment(user_id, applicant_id, workflow_step_id)
@user = User.find(user_id)
@organization = @user.organization
@applicant = Applicant.find(applicant_id)
@url = root_url + 'applicants/' + @applicant.id.to_s
@workflow_step = WorkflowStep.find(workflow_step_id)
mail(to: @user.email, subject: 'Applicant Assigned.')
end
end
里面的代码layouts/user_mailer.html.erb
<body style="background:#f4f4f4;">
<table width="100%" bgcolor="<%= @organization.background_color %>" cellpadding="0" cellspacing="0">
<tr>
<td>
<table align="center" width="730" cellpadding="0" cellspacing="0" >
sidekiq 控制台出错
2015-03-24T08:58:14Z 5595 TID-cjors WARN: {"retry"=>true, "queue"=>"default", "class"=>"Sidekiq::Extensions::DelayedMailer", "args"=>["---\n- !ruby/class 'UserMailer'\n- :notify_applicant_assignment\n- - 4\n - '9'\n - '9'\n"], "jid"=>"4421abf04e7e6864c7ee9fd8", "enqueued_at"=>1427187124.323067, "error_message"=>"undefined method `background_color' for nil:NilClass", "error_class"=>"ActionView::Template::Error", "failed_at"=>1427187124.3466575, "retry_count"=>4, "retried_at"=>1427187494.9246943}
2015-03-24T08:58:14Z 5595 TID-cjors WARN: undefined method `background_color' for nil:NilClass
2015-03-24T08:58:14Z 5595 TID-cjors WARN: /home/leapfrog/projects/ATS/app/views/layouts/user_mailer.html.erb:17:in `_app_views_layouts_user_mailer_html_erb__235114594899105140_70586860'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/template.rb:143:in `block in render'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/activesupport-4.0.4/lib/active_support/notifications.rb:161:in `instrument'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/template.rb:141:in `render'
/home/leapfrog/.rvm/gems/ruby-2.1.1/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
/
延迟扩展的工作方式很可能与您预期的不同。它们与常规异步作业不同。
在the documentation中明确指出:
I strongly recommend avoiding delaying methods on instances. This stores object state in Redis which can get out of date, causing stale data problems.
如果延迟扩展存储未呈现的模板和邮件参数(主题、收件人、发件人等)而不是为过时的实例调用方法本身,我不会感到惊讶。
此外,请注意 notify_applicant_assignment
未定义为 class 方法(它看起来像实例方法),尽管 Sidekiq documentation 声明延迟应应用于 class 方法。
Any class method can be delayed via the same methods as above:
我建议将 notify_applicant_assignment
设为 class 方法并重试(注意 def self.notify_applicant_assignment
与 [=15 中的 self =]).
class UserMailer < ActionMailer::Base
include Sidekiq::Worker
default from: CommonConstants::DO_NOT_REPLY_ADDRESS
layout 'user_mailer'
def self.notify_applicant_assignment(user_id, applicant_id, workflow_step_id)
@user = User.find(user_id)
@organization = @user.organization
@applicant = Applicant.find(applicant_id)
@url = root_url + 'applicants/' + @applicant.id.to_s
@workflow_step = WorkflowStep.find(workflow_step_id)
mail(to: @user.email, subject: 'Applicant Assigned.')
end
end
或者,我还建议在 applicants_controller.rb
中使用标准异步引擎:
UserMailer.notify_applicant_assignment_async(current_assigned_user.id, applicant, workflow_step)
请告诉我进展如何,如果还有需要,我可以再研究一下。
祝你好运!