2013-04-23 44 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,:受试者=> “感谢您注册”,:体=> “你好”) 端