2016-09-02 6 views
0

Rails 4アプリで2つのカスタムレイクタスクを作成しようとしています。彼らはagent_cardテーブルを読んで、ロジックに基づいて、時間が始まると電子メールを発します。これは私のはじめてのレーキ作業の作成であり、私はエラーが出ないので、私はそれを正しくやっているかどうかわかりません。私は、メーラーを使用して、メーラーが発射された後にレターオープナーが動作すると仮定します(すべて正常に動作するはずです)が、わかりません。私は以下のすべての関連コードを掲載しています。前もって感謝します!ActionMailer with Custom Rake Task

class LicenseExpireMailer < ActionMailer::Base 
    default from: "Mike <[email protected]>" 

    def license_expire_mgr(agent, agent_card) 
     @agent = agent 
     @agent_card = agent_card 
     # Head of Agent Development 
     mail to: "[email protected]", subject: "#{@agent.name}'s License Expiring" 
    end 
end 

私はrakeタスクをテストしようとすると、私は

rake agent_cards:license_expire_agent 

or 

rake agent_cards:license_expire_mgr 
を入力license_expire_mgr_mailer.rb

.rakeファイル

namespace :agent_cards do 
    desc 'Sends an email to an agent with a license expiring 2 months from today' 
    task license_expire_agent: :environment do 
     Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}" 
     AgentCard.all.each do |agent_card| 
     if agent_card.real_estate_license_expires_at == Date.today + 2.months 
      LicenseExpireMailer.license_expire_agent(@agent_card, @agent).deliver_later 
     end 
     end 
    end 
end 

namespace :agent_cards do 
    desc 'Sends an email to an the agent development manager when a license expires 1 week from today' 
    task license_expire_mgr: :environment do 
     Rails.logger.info "Mailer Method #{ActionMailer::Base.delivery_method}" 
     AgentCard.all.each do |agent_card| 
     if agent_card.real_estate_license_expires_at == Date.today + 7 
      LicenseExpireMailer.license_expire_mgr(@agent_card, @agent).deliver_later 
     end 
     end 
    end 
end 

license_expire_agent_mailer.rb

class LicenseExpireMailer < ActionMailer::Base 
    default from: "Mike <[email protected]>" 

    def license_expire_agent(agent, agent_card) 
     @agent = agent 
     @agent_card = agent_card 
     mail to: "[email protected]", subject: 'Your license is about to expire!' 
    end 
end 

エラー

NameError: uninitialized constant LicenseExpireMailer 
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:7:in `block (3  levels) in <top (required)>' 
/Users/michaelwiesenhart/.rvm/gems/[email protected]/gems/activerecord- 4.2.6/lib/active_record/relation/delegation.rb:46:in `each' 
/Users/michaelwiesenhart/.rvm/gems/[email protected]/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each' 
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:5:in `block (2 levels) in <top (required)>' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `each' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain' 
/Users/michaelwiesenhart/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `each' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/lib/rake/application.rb:75:in `run' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/gems/rake-11.1.2/bin/rake:33:in `<top (required)>' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `load' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/rake:23:in `<main>' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `eval' 
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1/bin/ruby_executable_hooks:15:in `<main>' 
Tasks: TOP => agent_cards:license_expire_agent 
+0

あなたのタスクは正しく見えます。あなたは 'deliver_later'の代わりに' deliver'を試して、すべてが動作しているかどうかをチェックするためにインスタント電子メールを取得できますか? – dp7

+0

@dkp私は変更を加えた、私は何も得ていない。 –

+0

それはあなたの '条件が実行されていないことを意味します。 – dp7

答えて

1

お使いのメーラーのファイル名は、Railsの慣例どおりメーラークラス名と一致しません。

メーラークラスは、この経路に配置する必要があります。

app/mailers/license_expire_mailer.rb 
関連する問題