我正在使用 Devise 在 Rails 4 应用程序中创建/记录用户,我遇到了几个覆盖 View 的问题。
我已经使用我的新 View 生成了设计 View 和 Controller ,它们工作正常。为了创建这些 View ,我使用了:
rails generate devise:views admins
当它尝试发送邮件时出现问题,它仍然使用设计默认 View :
app/views/devise/mailer/confirmation_instructions.html.erb
其余 View 已从我的/admins/sessions 和类似文件中正确加载。
我也在 devise.rb
中设置了 config.scoped_views = true
。
也尝试使用自定义邮件程序并将模板路径设置为我的 View ,但它都不起作用(它仍然尝试发送带有设计 View 的邮件):
default template_path: 'blablabla/admins/mailer'
知道哪里出了问题吗?
最佳答案
所以这并不完美,但它对我有用...
为来自自定义 Controller 的电子邮件版本传递 opts 参数
UserNotificationsMailer.confirmation_instructions(user, user.confirmation_token, { subject: "Please confirm your account.", version: "request" } ).deliver_later
然后在方法中设置一个实例变量
def confirmation_instructions(record, token, opts={})
@version = opts[:version]
super
end
并在 confirmation_instructions.html.erb 中使用该变量
<% if @version.nil? %>
<p>Welcome <%= @email %>!</p>
<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>
<% elsif @version == "request" %>
<p><b>From: location.org</b></p>
<p> <b>Subject:</b> <%= @subject %> </p>
<p>We noticed you signed up for an account...but have not confirmed yet.</p>
<p>Confirm you account now and get started!</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
<p> Respond to <%= mail_to(@email) %> </p>
<% elsif @version == "warn" %>
<p><b>From: location.org</b></p>
<p> <b>Subject:</b> <%= @subject %> </p>
<p>We noticed you signed up for an account ... confirmed soon it may be scheduled for removal.</p>
<p>Confirm you account now and get started!</p>
<p><%= link_to 'Confirm my account', confirmation_url(@resource, confirmation_token: @token) %></p>
<p> Respond to <%= mail_to(@email) %> </p>
<% end %>
没有猴子补丁,这很好,并发送不同的版本。仍在寻找完美的解决方案,但目前有效。
关于ruby-on-rails - 设计不使用自定义邮件 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32142561/