Rails 4.1 Mailer预览和设计定制电子邮件 [英] Rails 4.1 Mailer Previews and Devise custom emails

查看:102
本文介绍了Rails 4.1 Mailer预览和设计定制电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全新的Rails 4.1.1应用程序,我在定制Devise的电子邮件。我想让它们显示在新的Rails电子邮件预览功能上,所以我做了以下操作:



1)添加以下代码片段到我的 config / development.rb file:

  config.action_mailer.preview_path =#{Rails.root} /创建我的自定义Devise电子邮件 UserMailer  app / mailers / user_mailer.rb 

  UserMailer类Devise :: Mailer 
helper:application#提供对application_helper中定义的所有帮助器的访问。
包含Devise :: Controllers :: UrlHelpers#可选。例如。 `confirm_url`

layoutnotifications_mailer
end

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

  config.mailer ='UserMailer'

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

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

def reset_password_instructions
UserMailer.reset_password_instructions(User。首先,{})
end

def unlock_instructions
UserMailer.unlock_instructions(User.first,{})
end
end

到目前为止,这么好。看来我已经做了一切正确。但是,我尝试在/ rails / mailers / user_mailer / confirmation_instructions路由上查看 confirmation_instructions 电子邮件的预览,我收到以下错误:

$ b $对于#<#< Class:0x007fa02ab808e0>:0x007fa030fb7e80>的
$ b

 未定义的方法`confirmation_url' 

我的的确认代码confirm_url.html.erb 模板看起来像这样:

 <%= t(notifications.texts.greeting)+ @ user.display_name% > 

< p>您可以通过以下链接确认您的帐户电子邮件:< / p>

< p><%= link_to'确认我的帐户',confirm_url(@resource,:confirmation_token => @token)%>< / p>

我做错了什么?我想这是我错误的方式,我称之为 confirmation_url 方法。任何人都可以帮助我吗?

解决方案

我发现这个问题是因为我正在试图找出如何预览Devise的电子邮件。我复制了你的代码几乎完全正确,它对我来说很好。



我唯一不同的是删除行 layoutnotifications_mailer / code>从 UserMailer - 当我包含它我有一个错误消息缺少模板布局/ notifications_mailer 。如果你删除它会发生什么?



Devise :: Controllers :: UrlHelpers 绝对包含方法 confirm_url (您可以通过打开Rails控制台并运行来查看此内容,包括Devise :: Controllers :: UrlHelpers 然后 confirmation_url ,所以我怀疑问题是你的预览没有被 UserMailer 呈现,我不是确定为什么,但行 layoutnotifications_mailer可能是罪魁祸首。



你有一个类叫 code> NotificationsMailer ?包含 Devise :: Controllers :: UrlHelpers 在哪里可以解决您的问题?


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) Added the following snippet to my config/development.rb file:

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

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) Changed config/initializers/devise.rb to contain the following snippet:

config.mailer = 'UserMailer'

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

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>

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>

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?

解决方案

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.

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?

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.

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

这篇关于Rails 4.1 Mailer预览和设计定制电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆