Rails 4 Sidekiq 未初始化常量错误

问题描述 投票:0回答:2

我的应用程序有问题,当尝试在后台发送电子邮件时,我收到错误

uninitialized constant AnswersController::LazyDoer
,我不知道为什么它不起作用,有什么建议吗?

我的工人在

app/workers/lazy_doer.rb

这是我的控制器:

class AnswersController < ApplicationController
  before_action :authenticate_user!
  before_action :set_question, except: [:adding_likes,:accept]

  def create
    @answer = Answer.new(answer_params)
    @answer.user_id = current_user.id
    @answer.question_id = @question.id
    @question_owner = User.find(@question.user_id)
    
    if @answer.save
      LazyDoer.perform_async(@question_owner,current_user,@answer,@question)
      redirect_to question_path(@question), notice: "Answer was successfully created."
    else    
      render(:template => "questions/show", alert: "There was an error when adding answer.")
      
    end
  end

这是我的工人:

class LazyDoer
  include Sidekiq::Worker
  sidekiq_options retry: false
  
  def perform(question_owner,current_user,answer,question)
    @question_owner = question_owner
    @current_user = current_user
    @answer = answer
    @question = question
    UserMailer.send_email(@question_owner,@current_user,@answer,@question).deliver
  end
end

编辑:

我让我的 LazyDoer 工作人员完全运行,但现在我在通过它发送电子邮件时遇到问题。最重要的是,MAILER 无需 SIDEKIQ 即可完美运行。这是 sidekiq 内部的错误:

2014-07-30T19:28:38.479Z 4317 TID-amn3w LazyDoer JID-3e465606b1d5728181002af0 INFO: start 2014-07-30T19:28:38.480Z 4317 TID-amn3w LazyDoer JID-3e465606b1d5728181002af0 INFO: fail: 0.001 sec 2014-07-30T19:28:38.481Z 4317 TID-amn3w WARN: {"retry"=>false, "queue"=>"default", "class"=>"LazyDoer", "args"=>["[email protected]", "[email protected]", "#<Answer:0x000000045fd148>", "#<Question:0x000000045fe728>"], "jid"=>"3e465606b1d5728181002af0", "enqueued_at"=>1406748518.4762628} 2014-07-30T19:28:38.481Z 4317 TID-amn3w WARN: undefined method `email' for "[email protected]":String 2014-07-30T19:28:38.481Z 4317 TID-amn3w WARN: /home/mateusz/Pulpit/Aptana3_Workspace/challenge_app/app/mailers/user_mailer.rb:9:in `send_email'
这是我的邮件:

class UserMailer < ActionMailer::Base default from: "someemail" def send_email(question_owner,cur_user,answer,question) @question_owner = question_owner @cur_user = cur_user @answer = answer @question = question mail(to: @question_owner.email, subject: "Answer added to your question:") end def accepted_email(user,answer,question) @user = user @answer = answer @question = question mail(to: @user.email, subject: "Your answer has been accepted") end end
    
ruby-on-rails-4 sidekiq ruby-on-rails-4.1
2个回答
1
投票
我有解决方案,Sidekiq错误的问题是,由于它使用的是redis的nosql数据库,redis无法正确理解复杂的rails数据,例如ActiveRecord模型,如果你试图发送给你的工作人员,比如说,整个用户拥有他所拥有的每一个属性 - 这在 Redis 中不起作用,数据太复杂了。解决方案很简单,准确查看生成的电子邮件视图和 mailer.rb 并准确查看您需要哪些属性,然后当您需要调用您的工作人员时,仅向他发送这些属性,而不是发送整个 ActiveRecord 模型。

这里有固定工人:

class LazyDoer include Sidekiq::Worker sidekiq_options retry: false def perform(question_owner_email,current_user_name,answer_contents,question_title) UserMailer.send_email(question_owner_email,current_user_name,answer_contents,question_title).deliver end end

固定控制器(最重要):

class AnswersController < ApplicationController before_action :authenticate_user! before_action :set_question, except: [:adding_likes,:accept] def create @answer = Answer.new(answer_params) @answer.user_id = current_user.id @answer.question_id = @question.id @question_owner = User.find(@question.user_id) if @answer.save LazyDoer.perform_async(@question_owner.email,current_user.name,@answer.contents,@question.title) #DLA MAILERA BEZ SIDEKIQ UserMailer.send_email(@question_owner,current_user,@answer,@question).deliver redirect_to question_path(@question), notice: "Answer was successfully created." else #redirect_to question_path(@question), alert: "There was an error when adding answer." render(:template => "questions/show", alert: "There was an error when adding answer.") end end end

固定用户邮件:

class UserMailer < ActionMailer::Base default from: "[email protected]" def send_email(question_owner_email,cur_user_name,answer_contents,question_title) @question_owner_email = question_owner_email @cur_user_name = cur_user_name @answer_contents = answer_contents @question_title = question_title mail(to: @question_owner_email, subject: "Answer added to your question:") end end

修复了电子邮件视图(使用 slim 模板语言而不是 erb):

doctype html html head meta content="text/html; charset=UTF-8" http-equiv="Content-Type" body h1 Your question #{@question_title} has been answered p | Answered by #{@cur_user_name} <br /> The answer content is: <br /> #{@answer_contents} p Accept or like the answer if it was useful for you.
    

1
投票
不要在 @question_owner 中发送用户对象,而是发送用户 ID。

© www.soinside.com 2019 - 2024. All rights reserved.