带有ActionMailer的Rake任务 - 未定义的方法错误

时间:2016-09-06 19:32:53

标签: ruby-on-rails postgresql actionmailer rake-task

我有一个自定义rake任务,我正在使用邮件程序执行此任务。我有一个包含所有代理商信息的代理表。我还有一个agent_card表,其中包含所有许可证数据。 agent_card表还有一个agent_id列,用于标识哪个卡与哪个代理程序一起使用。当试图运行rake任务并发送邮件时,我不断收到“ActionView :: Template :: Error:undefined方法`name'为nil:NilClass。”我无法弄清楚如何通过agent_card表获取名称,电子邮件和电话。我已经包含了下面的所有代码。拜托,谢谢!

错误:

rake aborted!
ActionView::Template::Error: undefined method `name' for nil:NilClass
/Users/michaelwiesenhart/Code/app/views/license_expire_mailer/license_expi re_mgr.html.erb:7:in   `_app_views_license_expire_mailer_license_expire_mgr_html_erb___944550213720770297_70323524541360'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/template.rb:145:in `block in render'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/notifications.rb:166:in `instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/template.rb:333:in `instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/template.rb:143:in `render'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in `block in instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in `instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/template_renderer.rb:52:in `render_template'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/template_renderer.rb:14:in `render'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/renderer.rb:46:in `render_template'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/renderer/renderer.rb:27:in `render'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/rendering.rb:100:in `_render_template'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/rendering.rb:83:in `render_to_body'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionpack-4.2.6/lib/abstract_controller/rendering.rb:25:in `render'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:904:in `block in collect_responses'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:918:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:918:in `each_template'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:900:in `collect_responses'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:829:in `mail'
/Users/michaelwiesenhart/Code/app/mailers/license_expire_mailer.rb:14:in `license_expire_mgr'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionpack-4.2.6/lib/abstract_controller/base.rb:198:in `process_action'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionpack-4.2.6/lib/abstract_controller/callbacks.rb:20:in `block in process_action'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:88:in `__run_callbacks__'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:81:in `run_callbacks'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionpack-4.2.6/lib/abstract_controller/callbacks.rb:19:in `process_action'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionpack-4.2.6/lib/abstract_controller/base.rb:137:in `process'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionview-4.2.6/lib/action_view/rendering.rb:30:in `process'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:596:in `block in process'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in `block in instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activesupport-4.2.6/lib/active_support/notifications.rb:164:in `instrument'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:593:in `process'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/base.rb:584:in `initialize'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/message_delivery.rb:25:in `new'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/message_delivery.rb:25:in `__getobj__'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/message_delivery.rb:34:in `message'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/actionmailer-4.2.6/lib/action_mailer/message_delivery.rb:85:in `deliver_now'
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:17:in `block (3 levels) in <top (required)>'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/.rvm/gems/ruby-2.3.1@global/gems/activerecord-4.2.6/lib/active_record/relation/delegation.rb:46:in `each'
/Users/michaelwiesenhart/Code/lib/tasks/license_expiration.rake:15:in `block (2 levels) in <top (required)>'
/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>'

代理商型号:

has_one :agent_card

AgentCard模型:

belongs_to :agent

license_expire_mgr.html.erb

Hiya, <br><br>

The agent listed below has a license that will expire one week from today. Please make sure they renew before it expires!<br><br>

<strong>Name:</strong> <%= @agent.name %><br>
<strong>Phone:</strong> <%= @agent.phone %><br>
<strong>Email:</strong> <%= @agent.email %><br><br>

license_expire_mailer.rb

class LicenseExpireMailer < ActionMailer::Base
   default from: "Mike <help@mike.com>"

   def license_expire_mgr(agent_card, agent)
   @agent_cards = agent_card
   @agent = agent
   mail to: "mike@mike.com", subject: "Agent License Expiring"
  end
end

2 个答案:

答案 0 :(得分:1)

@agent未传递给邮件程序,或者其值已被评估为nil

问题不在于你的rake任务或邮件程序,弄清楚为什么param是nil并且这将解决你的问题。

答案 1 :(得分:0)

我明白了。我试图使用agent_card并且不需要它。一旦我删除它,它就可以正常工作!