2014-10-18 40 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