1

使用 Devise 3 和 Rails 4,我想在我的确认电子邮件中添加一张图片。

我试过了...

Views/Devise/Mailer/confirmation_instructions.html.erb

<%= image_tag("my_image.png") %>

...但是图像显示为默认占位符问号。

我应该如何正确访问资产?

4

2 回答 2

5

还有另一种在设计确认电子邮件中提供图像的方法 - 将其附加到信中。

请参阅说明:

1. 创建自己的邮件:

应用程序/mailers/my_devise_mailer.rb

class MyDeviseMailer < Devise::Mailer

  helper :application # gives access to all helpers defined within `application_helper`.
  include Devise::Controllers::UrlHelpers # Optional. eg. `confirmation_url`
  default template_path: 'devise/mailer' # to make sure that your mailer uses the devise views

  def confirmation_instructions(record, token, opts={})
    attachments.inline['logo.jpg'] = File.read("app/assets/images/logo.jpg")
    super
  end

end

2. 在您的config/initializers/devise.rb中,设置config.mailer="MyDeviseMailer".

3. 在你的app/views/devise/mailer/confirmation_instructions.html.erb使用

<p><%= image_tag(attachments['logo.jpg'].url, width: '120', height: '120') %></p>
于 2016-09-02T15:00:59.883 回答
2

代替

<%= image_tag("my_image.png") %>

<%= image_tag "http://mydomain.com/path/to/my_image.png" %>

例如:

<%= image_tag "https://www.google.com/images/srpr/logo11w.png" %>

上面的示例将在您的电子邮件正文中添加 Google 徽标。

如果您希望显示自己的图像,请替换

mydomain.com: 你的域名

/path/tomy_image.png:您所在文件夹的路径

于 2014-04-02T18:09:16.907 回答