我收到错误
undefined method `url' for nil:NilClass
当我只预览我的Devise邮件程序时。我正在尝试在电子邮件中添加内嵌图片。
# app/mailers/user_mailer.rb
class UserMailer < Devise::Mailer
include Devise::Controllers::UrlHelpers
helper :application
before_action :add_inline_attachment!
def reset_password_instructions(record, token, opts={})
super
end
#...
private
def add_inline_attachment!
attachments.inline['logo.jpg'] = File.read('app/assets/images/logo.jpg')
end
end
在我的邮件程序视图:
中# app/views/user/mailer/reset_password_instructions.html.erb
<%= image_tag attachments['logo.jpg'].url %>
在设置Devise时,我添加了以下配置以获得范围视图
# config/initializers/devise.rb
config.mailer = 'UserMailer'
config.scoped_views = true
我的头发拉动的主要部分是这适用于包括devise_invitable
在内的每个邮件浏览器。这是核心设计视图password_change
,reset_password_instructions
和email_change
。
# app/views/user/mailer/reset_password_instructions.html.erb
<%= image_tag attachments['logo.jpg'].try(:url) %>
返回nil is not a valid asset source
我也试过直接在里面添加附件:
# app/mailers/user_mailer.rb
class UserMailer < Devise::Mailer
include Devise::Controllers::UrlHelpers
helper :application
#...
def reset_password_instructions(record, token, opts={})
attachments.inline['logo.jpg'] = File.read('app/assets/images/logo.jpg')
super
end
#...
end
另外,添加图像的根目录:
# Both
attachments.inline['logo.jpg'] = File.read("#{Rails.root}/app/assets/images/logo.jpg")
# And
attachments.inline['logo.jpg'] = File.read(Rails.root.join("app/assets/images/logo.jpg"))