elegant way to File.read assets in development

时间:2018-04-20 00:38:47

标签: ruby-on-rails sprockets

I have the following code

module EmailHelper
  def email_image_tag(image, **options)
    attachments.inline[image] = File.read(image_path(image))
    image_tag attachments[image].url, **options
  end
end

In production this references the correct image with hash, because the image asset is precompiled, however in development this throws a file read exception.

Is there an elegant way to do a File.read without having to check on if Rails.env.development?

1 个答案:

答案 0 :(得分:0)

这里最大的误解是我认为我应该指出一个消化的资产,忘记这些资产是为了服务而不是作为文件读取。因此,可以从资产地图中完美地加载文件。我解决了上面的问题如下:

module MailHelper
  def mail_image_tag(image, **options)
    path = Rails.root.join('app', 'assets', 'images', image)

    attachments.inline[image] = File.read(image)
    image_tag attachments[image].url, **options
  end
end