2015-06-22 142 views
14

你好,
梅勒錯誤缺少模板

我有問題,的ActionMailer,當我嘗試執行操作:

rake send_email 

我得到一個錯誤:

rake aborted! 
ActionView::MissingTemplate: Missing template user_mailer/mailer with "mailer". Searched in: 
    * "user_mailer" 

這是我的:

寄件人/ user_mailer.rb

class UserMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def mailer(user) 
    @user = user 
    mail(to: @user.email, subject: 'Test') 
    end 

end 

視圖/ user_mailer文件/ mailer.html.erb

<!DOCTYPE html> 
<html> 
    <head> 
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' /> 
    </head> 
    <body> 
    <p> 
     Sample mail. 
    </p> 
    </body> 
</html> 

視圖/ user_mailer文件/ mailer.text.erb

Sample mail. 

lib/tasks/emails _task.rake

desc 'send email' 
task send_email: :environment do 
    UserMailer.mailer(User.last).deliver! 
end 

配置/環境/ development.rb

# I recommend using this line to show error 
    config.action_mailer.raise_delivery_errors = true 

    # ActionMailer Config 
    config.action_mailer.delivery_method = :letter_opener 

# config.action_mailer.default_url_options = { :host => 'localhost:3000' } 
# config.action_mailer.delivery_method = :smtp 
# SMTP settings for gmail 
config.action_mailer.smtp_settings = { 
:address    => "smtp.gmail.com", 
:port     => 587, 
:user_name   => ENV['gmail_username'], 
:password    => ENV['gmail_password'], 
:authentication  => "plain", 
:enable_starttls_auto => true 
} 

# Send email in development mode? 
config.action_mailer.perform_deliveries = true 

我搜索計算器上的解決方案,我嘗試了許多問題的答案對於類似的問題,但不幸的是他們沒有爲我工作。

我找到了解決辦法,當我補充身體到郵件的方法,如:

def mailer(user) 
    @user = user 
    mail(to: @user.email, subject: 'Test', body: 'something') 
end 

然後,它的工作,但我想有單獨的文件體,使其與用戶名和其他事情更復雜。

如果有人有一個想法如何解決這個問題,那麼我會非常感激:)

+2

我發現了同樣的問題,並且您的修復工作正常(儘管我現在仍然偶爾會出現故障。)有趣的是,這隻發生在我的生產環境中。開發工作得很好。我仍在試圖弄清楚發生了什麼事。 –

+0

我也有同樣的問題!剛剛開始賞金:-) – THpubs

+0

你用什麼來運行這個?嘗試像這樣運行命令'RAILS_ENV = production rake send_email' – THpubs

回答

0

你可以發佈一個最低限度的Github上庫重現你的錯誤?

您可以嘗試生成您的郵件,以檢查你是不是忘了什麼東西:

bundle exec rails generate mailer mailer_classname mailer_viewname

你的情況:

bundle exec rails generate mailer user_mailer mailer

3

嘗試增加布局

class UserMailer < ActionMailer::Base 
    default from: "[email protected]" 

    layout "mailer" 

    def mailer(user) 
    @user = user 
    mail(to: @user.email, subject: 'Test') 
end 

end 
1

這是很可能Rails只是沒有找到你新創建的文件。如果你有spring寶石運行,那可能是問題。(更多關於春:https://github.com/rails/spring

要設置終端的命令和停止運行spring

$ bundle exec spring binstub --all 
$ bin/spring stop 

我想這一點,然後重新運行與rails s我的應用程序。這不起作用,然後嘗試徹底關閉終端並重新啓動計算機。運行rails s,並在第二個選項卡嘗試測試與rails c

mail = UserMailer.mailer(User.last) 

最後爲我工作,希望它會幫助你們中的一些!

(其中一些步驟可能是多餘的。)

1

我重命名了我的方法,它工作正常。也許該方法的名稱不能是電子郵件或郵寄...

+1

你能具體嗎?什麼方法? –

2
class UserMailer < ActionMailer::Base 
    default from: "[email protected]" 

    layout "mailer" 

    def mailer(user) 
    @user = user 
    mail(to: @user.email, subject: 'Test') 
end 

end 

下佈局#添加HTML佈局

mailer.html.erb

<!DOCTYPE html> 
<html> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <style> 
     /* Email styles need to be inline */ 
    </style> 
    </head> 

    <body> 
    <%= yield %> 
    </body> 
</html> 

下佈局#添加文本佈局

mailer.text.erb

<%= yield %> 

使用預覽檢查您的電子郵件根據您的測試fram ework。

0

我有同樣的問題,我的解決方案是刪除郵件模板文件,在您的情況下views/user_mailer/mailer.html.erb並再次創建它。 似乎我創建了一個名字正確的文件,但在某處存在一些奇怪的空白,ActionMailer沒有識別它。

1

我有同樣的問題。重新啓動sidekiq爲我解決它。