为什么我的实例变量没有在我的操作邮件程序的视图中设置?

发布于 2024-12-28 04:59:00 字数 927 浏览 0 评论 0原文

由于某种原因,我的实例变量没有在此 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初相遇 2025-01-04 04:59:00

事实证明这一行:

return unless user and user.email and not user.confirmed_at

没有中止发送电子邮件。相反,它只是中止您在上面看到的 UserMailer 代码,并直接进入现在引用尚未设置的实例变量的视图。因此本质上 ActionMailer 的工作方式与常规控制器完全相同。

为了解决此问题,我已将检查是否将电子邮件发送到运行 UserMailer 命令的位置的条件移至了位置。

Turns out this line:

return unless user and user.email and not user.confirmed_at

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.

初熏 2025-01-04 04:59:00

您必须重新启动您的服务和工作人员(如果有)才能反映更改。

You will have to restart your services and Workers if any for the changes to reflect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文