2014-10-18 39 views
2

这有点令人费解,在生产或开发中运行此代码时,Rails不会抱怨找不到模板,但是当我运行进入测试模式这一点,它给我一个错误说,它不能找到一个“邮件”模板......缺少ActionMailer的'模板',但仅在测试模式下运行时...直接调用ActionMailer基类

ActionMailer::Base.mail(:from => '[email protected]', :to => "[email protected]", :subject => 'some test', body: 'some body).deliver 

我甚至试图与格式块玩,指定{渲染文本: 'x'},对于.html和.text,但没有运气。

这是Rails 4.

谢谢!

回答

3

好的,我发现这个问题,因为rails是关于约定的,所以当传递给邮件的“BODY”是零时,它会尝试从类名中猜测模板路径名和视图名。所以即直接使用这种方法。我不得不调试rails框架来找出这个问题:

I.e.关于gems/actionmailer-4.1.6/lib/action_mailer/base.rb:

这个方法是问题的答案,你可以看到如果body eval为nil,那么它就是猜测路径名的路线,等:

def collect_responses(headers) #:nodoc: 
     responses = [] 

     if block_given? 
     collector = ActionMailer::Collector.new(lookup_context) { render(action_name) } 
     yield(collector) 
     responses = collector.responses 
     elsif headers[:body] 
     responses << { 
      body: headers.delete(:body), 
      content_type: self.class.default[:content_type] || "text/plain" 
     } 
     else 
     templates_path = headers.delete(:template_path) || self.class.mailer_name 
     templates_name = headers.delete(:template_name) || action_name 

     each_template(Array(templates_path), templates_name) do |template| 
      self.formats = template.formats 

      responses << { 
      body: render(template: template), 
      content_type: template.type.to_s 
      } 
     end 
     end 

     responses 
    end