質問

私のアプリケーションに問題がありますが、バックグラウンドで電子メールを送信しようとするとエラーが発生しました。

私の労働者はuninitialized constant AnswersController::LazyDoer

にあります

これは私のコントローラーです:

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"=>["matthew.kilan@gmail.com", "matthew.kilan@gmail.com", "#<Answer:0x000000045fd148>", "#<Question:0x000000045fe728>"], "jid"=>"3e465606b1d5728181002af0", "enqueued_at"=>1406748518.4762628}
2014-07-30T19:28:38.481Z 4317 TID-amn3w WARN: undefined method `email' for "matthew.kilan@gmail.com":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: "matthew.kilan@gmail.com"

  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
.

役に立ちましたか?

解決

ソリューション、SideKiQエラーの問題は、RedisであるNoSQLデータベースを使用しているため、Redisは、例えばActiveRecordモデルのように複雑なRailsデータのようなものではありません。労働者に送信しようとしている場合は、労働者に送信しようとしています。彼が持っているすべての単一の属性を持つ全ユーザーはRedisでは機能しません、データは複雑すぎることです。解決策は簡単で、あなたの生成された電子メールビューとmayer.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: "matthew.kilan@gmail.com"

  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
.

定電子メールビュー(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.
.

他のヒント

Instead of sending user object in @question_owner send user id.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top