2013-04-23 65 views
1

我試圖建立SendGrid在我的Rails應用程序,我一直在關注這樣的:http://sendgrid.com/docs/Integrate/Frameworks/rubyonrails.htmlSendGrid和Rails錯誤

到目前爲止,我安裝我的config/environment.rb

ActionMailer::Base.smtp_settings = { 
    :user_name => *********, 
    :password => *********, 
    :domain => *********, 
    :address => "smtp.sendgrid.net", 
    :port => 587, 
    :authentication => :plain, 
    :enable_starttls_auto => true 
} 

發新的文件所謂app/models/notifier.rb其中包含

class Notifier < ActionMailer::Base 
    include SendGrid 
    default :from => "*****@******.com" 

    # send a signup email to the user, pass in the user object that contains the user's email address 
    def signup_email(user) 
    mail(:to => user.email, :subject => "Thanks for signing up") 
    end 
end 

在軌控制檯,這是發生了什麼。

>> @new_user = User.new(:email => '[email protected]') 
=> #<User id: nil, email: "[email protected]", created_at: nil, updated_at: nil> 
>> @new_user.save 
    (0.2ms) BEGIN 
    SQL (1.0ms) INSERT INTO "users" ("created_at", "email", "updated_at") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Tue, 23 Apr 2013 01:21:17 UTC +00:00], ["email", "[email protected]"], ["updated_at", Tue, 23 Apr 2013 01:21:17 UTC +00:00]] 
    (1.8ms) COMMIT 
=> true 
>> Notifier.signup_email @new_user 
ActionView::MissingTemplate: Missing template notifier/signup_email with "mailer". Searched in: 
* "notifier" 

我在做什麼錯?我不明白爲什麼我需要一個模板。我是Rails新手,所以請原諒我,如果這是顯而易見的。

回答

2

問題是ActionMailer默認使用Rails中的模板。你應該檢查出the mailer guide for rails,它比我在這裏要描述的更多細節。

要解決這個問題,您應該在app/views/notifier/signup_email.html.erb的某些內容中創建一個模板。


順便說一句,如果你想發送一封電子郵件,而無需使用模板,你需要創建郵件後立即調用deliver功能。詳情請參閱此堆棧溢出問題:

How can I send mail with rails without a template?

+0

由於尋求幫助!可能值得注意的是,在我的郵件調用中包含':body =>「blah blah」'之前,我一直在接收模板錯誤。否則,它會尋找一個模板來填充正文。 – 2013-04-23 02:25:16

0

高清signup_email(用戶) 郵件(:到=> user.email,:主題=> 「感謝您註冊」) 結束

通過

DEF signup_email(用戶) 郵件替換(:到=> user.email,:受試者=> 「感謝您註冊」,:體=> 「你好」) 端