【问题标题】:Add an image to confirmation email using Devise 3 and Rails 4使用 Devise 3 和 Rails 4 将图像添加到确认电子邮件
【发布时间】:2014-04-02 17:47:45
【问题描述】:

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

我试过了……

Views/Devise/Mailer/confirmation_instructions.html.erb

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

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

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

【问题讨论】:

    标签: image ruby-on-rails-4 devise asset-pipeline devise-confirmable


    【解决方案1】:

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

    查看说明:

    1。 创建您自己的邮件:

    app/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>
    

    【讨论】:

      【解决方案2】:

      替换

      &lt;%= image_tag("my_image.png") %&gt;

      &lt;%= image_tag "http://mydomain.com/path/to/my_image.png" %&gt;

      例如:

      &lt;%= image_tag "https://www.google.com/images/srpr/logo11w.png" %&gt;

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

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

      mydomain.com : 你的域名

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

      【讨论】:

      • 如果图片位于 app/assets/images 中,您能否解释一下如何提供图片。然后如何使用 image_tag 链接到它?
      • @moeabdol 你想发布一个新的 SO 问题,我可以为你回答吗?
      • 应该是&lt;%= image_tag "http://www.yourdomiain.com/assets/images/logo.png" %&gt;
      猜你喜欢
      • 2016-04-21
      • 2012-08-10
      • 2012-04-21
      • 2011-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多