Rails 4.1 邮件程序预览和设计自定义电子邮件

编程入门 行业动态 更新时间:2024-10-22 19:30:00
本文介绍了Rails 4.1 邮件程序预览和设计自定义电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个全新的 Rails 4.1.1 应用程序,我正在其中自定义设计电子邮件.我想让它们显示在新的 Rails 电子邮件预览功能上,因此我执行了以下操作:

I have a brand new Rails 4.1.1 app where I'm customizing the Devise emails. I want to have them displayed on the new Rails email preview feature so I did the following:

1) 将以下代码段添加到我的 config/development.rb 文件中:

1) Added the following snippet to my config/development.rb file:

config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"

2) 在 app/mailers/user_mailer.rb 中创建我的自定义设计电子邮件 UserMailer:

2) Created my custom Devise email UserMailer in app/mailers/user_mailer.rb:

class UserMailer < Devise::Mailer helper :application # gives access to all helpers defined within `application_helper`. include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url` layout "notifications_mailer" end

3) 将 config/initializers/devise.rb 更改为包含以下代码段:

3) Changed config/initializers/devise.rb to contain the following snippet:

config.mailer = 'UserMailer'

4) 将 UserMailerPreview 类添加到 lib/mailer_previews 中,内容如下:

4) Added the class UserMailerPreview to lib/mailer_previews with the following content:

class UserMailerPreview < ActionMailer::Preview def confirmation_instructions UserMailer.confirmation_instructions(User.first, {}) end def reset_password_instructions UserMailer.reset_password_instructions(User.first, {}) end def unlock_instructions UserMailer.unlock_instructions(User.first, {}) end end

到目前为止,一切都很好.看起来我已经做对了一切.但后来我尝试在/rails/mailers/user_mailer/confirmation_instructions 路由上查看 confirmation_instructions 电子邮件的预览,但出现以下错误:

So far, so good. Looks like I've done everything right. But then I try to see the preview for the confirmation_instructions email at the /rails/mailers/user_mailer/confirmation_instructions route and I get the following error:

undefined method `confirmation_url' for #<#<Class:0x007fa02ab808e0>:0x007fa030fb7e80>

我的 confirmation_url.html.erb 模板的代码如下所示:

the code for my confirmation_url.html.erb template looks like this:

<%= t("notifications.texts.greeting") + @user.display_name %>, <p>You can confirm your account email through the link below:</p> <p><%= link_to 'Confirm my account', confirmation_url(@resource, :confirmation_token => @token) %></p>

我做错了什么?我想我调用 confirmation_url 方法的方式有问题.任何人都可以在这里帮助我吗?

What am I doing wrong? I guess it is something wrong with the way I call the confirmation_url method. Anyone can help me here?

推荐答案

我发现这个问题是因为我试图弄清楚如何自己预览 Devise 电子邮件.我几乎完全复制了你的代码,对我来说效果很好.

I found this question because I was trying to figure out how to preview Devise emails myself. I copied your code almost exactly and it works fine for me.

我唯一不同的是从 UserMailer 中删除了行 layout "notifications_mailer" - 当我包含它时,我收到一条错误消息 Missing template layouts/notifications_mailer.如果删除它会怎样?

The only thing I did differently was to remove the line layout "notifications_mailer" from UserMailer - when I included it I got an error message Missing template layouts/notifications_mailer. What happens if you remove it?

行 Devise::Controllers::UrlHelpers 肯定包含方法 confirmation_url(您可以通过打开 Rails 控制台并运行 include 亲眼看到这一点Devise::Controllers::UrlHelpers 然后是 confirmation_url,所以我怀疑问题是你的预览根本没有被 UserMailer 渲染.我是不知道为什么,但行 layout "notifications_mailer" 可能是罪魁祸首.

The line Devise::Controllers::UrlHelpers definitely includes the method confirmation_url (you can see this for yourself by opening up the Rails console and running include Devise::Controllers::UrlHelpers then confirmation_url, so I suspect the problem is that your previews aren't being rendered by UserMailer at all. I'm not sure why, but the line layout "notifications_mailer" might be the culprit.

你有一个名为 NotificationsMailer 的类吗?在其中包含 Devise::Controllers::UrlHelpers 是否可以解决您的问题?

Do you have a class called NotificationsMailer? Does including Devise::Controllers::UrlHelpers in there solve your problem?

更多推荐

Rails 4.1 邮件程序预览和设计自定义电子邮件

本文发布于:2023-10-28 01:05:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535046.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   电子邮件   邮件   程序   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!