rod*_*ves 35 ruby ruby-on-rails devise ruby-on-rails-4
我有一个全新的Rails 4.1.1应用程序,我正在自定义Devise电子邮件.我想让它们显示在新的Rails电子邮件预览功能上,所以我做了以下事情:
1)在我的config/development.rb文件中添加了以下代码段:
config.action_mailer.preview_path = "#{Rails.root}/lib/mailer_previews"
Run Code Online (Sandbox Code Playgroud)
2)创建我的自定义设计的电子邮件UserMailer中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
Run Code Online (Sandbox Code Playgroud)
3)更改config/initializers/devise.rb为包含以下代码段:
config.mailer = 'UserMailer'
Run Code Online (Sandbox Code Playgroud)
4)添加了类UserMailerPreview以lib/mailer_previews用下面的内容:
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
Run Code Online (Sandbox Code Playgroud)
到现在为止还挺好.看起来我已经做好了一切.但后来我尝试confirmation_instructions在/ rails/mailers/user_mailer/confirmation_instructions路线上看到电子邮件的预览,我收到以下错误:
undefined method `confirmation_url' for #<#<Class:0x007fa02ab808e0>:0x007fa030fb7e80>
Run Code Online (Sandbox Code Playgroud)
我的confirmation_url.html.erb模板的代码如下所示:
<%= 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>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我想我调用confirmation_url方法的方式有问题.有人可以帮我吗?
ste*_*eel 51
对于那些希望在不使用自定义邮件程序的情况下预览Devise电子邮件的人(但仍然是自定义电子邮件),这就是我所做的:
配置您的应用以进行电子邮件预览.
设置Devise Mailer Preview类
一个.Rails~> 4.1
# mailer/previews/devise_mailer_preview.rb
class Devise::MailerPreview < ActionMailer::Preview
def confirmation_instructions
Devise::Mailer.confirmation_instructions(User.first, "faketoken")
end
def reset_password_instructions
Devise::Mailer.reset_password_instructions(User.first, "faketoken")
end
...
end
Run Code Online (Sandbox Code Playgroud)
湾 Rails~> 5.0
class DeviseMailerPreview < ActionMailer::Preview
... # same setup as Rails 4 above
Run Code Online (Sandbox Code Playgroud)重启服务器
当您忘记在模型中包含必要的设计模块时,您将获得设计URL助手的未定义方法.例如,如果您的模型是User并且您想要confirmation_url,则必须确保包含该:confirmable模块:
devise <...snipped...>, :confirmable
Run Code Online (Sandbox Code Playgroud)
使用Rails 5并发现语法与@ steel的优秀答案略有不同,使用双"::"作为区别:
# Preview all emails at http://localhost:3000/rails/mailers/devise_mailer
class DeviseMailerPreview < ActionMailer::Preview
def reset_password_instructions
Devise::Mailer.reset_password_instructions(User.first, "faketoken")
end
end
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题,因为我试图弄清楚如何自己预览Devise电子邮件.我几乎完全复制了你的代码,它对我来说很好.
我做了不同的唯一的事情是消除线路layout "notifications_mailer"从UserMailer-当我把它我得到一个错误信息Missing template layouts/notifications_mailer.如果删除它会发生什么?
该生产线Devise::Controllers::UrlHelpers肯定包括方法confirmation_url(可以通过打开Rails的控制台,并运行此看到自己include Devise::Controllers::UrlHelpers那么confirmation_url,让我怀疑的问题是,你的体验是不被渲染UserMailer的.我不知道为什么,但线layout "notifications_mailer"可能是罪魁祸首.
你有课NotificationsMailer吗?包括Devise::Controllers::UrlHelpers在那里解决你的问题?