【发布时间】:2011-05-11 17:32:02
【问题描述】:
我在这里关注this Railscast,从我的 Rails 应用程序发送邮件。
起初我尝试像教程中那样设置邮件拦截器,但我已经放弃了,因为我总是有一个可爱的can't convert nil into Hash (TypeError)。
现在我省略了拦截器,我想实际发送一封邮件,但你猜怎么着 - 我的老朋友回来了:
can't convert nil into Hash (TypeError)
app/mailers/user_mailer.rb:20:in `registration'
这是 mailer 类(第 20 行是调用 mail 的类):
class UserMailer < ActionMailer::Base
def registration( user )
@invited = user
subject = 'Welcome!'
if Rails.env.development?
to = 'my.private@email.com'
subject = "to #{@invited.email}: #{subject}"
else
to = @invited.email
end
logger.info "Sending email to #{to} with subject #{subject}"
mail( :from => 'noreply@mysite.com', :to => to, :subject => subject )
end
end
日志显示:
invite entered for user foo@bar.com
Sending email to foo@bar.com
Sending email to my.private@email.com with subject to foo@bar.com: Welcome!
这是请求的视图文件 (registration.text.erb):
Welcome!
blablah blah click the following link to activate blah blah <%= activate_user_path( @invited )%> blah blah.
blah blah this is actually german (but plaintext) encoded in utf-8.
Viel Vergnügen beim Verwenden des Systems!
感谢任何帮助
【问题讨论】:
-
我认为你的
user是nil -
#20 是调用 mail(...) 的那个
-
nope user is not nil here, log 实际上显示了正确的数据。
-
你是如何从控制器调用
registration方法的? -
控制器在用户对象上调用invite(),它会执行一些用户操作,然后调用
UserMailer.registration( self ).deliver
标签: ruby-on-rails-3 actionmailer