为什么我的实例变量没有在我的操作邮件程序的视图中设置?
由于某种原因,我的实例变量没有在此 ActionMailer 中设置。其他 ActionMailers 似乎工作正常。有什么想法可能导致这种情况吗? UserMailer 是限制词吗?
使用导轨3.1
# app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: 'My Name <[email protected]>'
def registration_confirmation(user)
# Don't send email if we don't have an email address to send it to or the user is already confirmed
return unless user and user.email and not user.confirmed_at
# Set global variable for mail view
@my_message = 'Hello world!'
# Send email
mail(to: "#{user.name} <#{user.email}>", subject: 'Please confirm your email address')
end
end
# app/views/user_mailer/registration_confirmation.text.erb
Hello who? <%= @my_message %> # Simply returns 'Hello who? '
For some reason my instance variables aren't being set in this ActionMailer. Other ActionMailers seem to work fine. Any ideas what might be causing this? Is UserMailer a restricted word perpahps?
Using rails 3.1
# app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: 'My Name <[email protected]>'
def registration_confirmation(user)
# Don't send email if we don't have an email address to send it to or the user is already confirmed
return unless user and user.email and not user.confirmed_at
# Set global variable for mail view
@my_message = 'Hello world!'
# Send email
mail(to: "#{user.name} <#{user.email}>", subject: 'Please confirm your email address')
end
end
# app/views/user_mailer/registration_confirmation.text.erb
Hello who? <%= @my_message %> # Simply returns 'Hello who? '
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实证明这一行:
没有中止发送电子邮件。相反,它只是中止您在上面看到的 UserMailer 代码,并直接进入现在引用尚未设置的实例变量的视图。因此本质上 ActionMailer 的工作方式与常规控制器完全相同。
为了解决此问题,我已将检查是否将电子邮件发送到运行 UserMailer 命令的位置的条件移至了位置。
Turns out this line:
did not abort sending the email. Instead it just aborts the UserMailer code you see above and goes straight to the view which is now referring to an instance variable which hasn't been set. So in essence an ActionMailer works exactly like a regular controller.
To solve this issue I've moved the conditions that check whether to send the email to the place where I run the UserMailer command.
您必须重新启动您的服务和工作人员(如果有)才能反映更改。
You will have to restart your services and Workers if any for the changes to reflect.