使用 prawnto_2 gem 加载图像时,RAILS_ROOT 不再有效 [英] RAILS_ROOT not longer valid when loading images with the prawnto_2 gem

查看:72
本文介绍了使用 prawnto_2 gem 加载图像时,RAILS_ROOT 不再有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用从 Rails 3.0 升级到 Rails 3.1.

I'm in the process of upgrading my app from Rails 3.0 to Rails 3.1.

我尽可能地将旧插件装箱以支持 gems,其中包括用这个漂亮闪亮的新插件替换旧的 prawnto 插件 prawnto_2 宝石.

I've binned my old plugins in favour of gems where possible and that includes replacing the old prawnto plugin with this nice shiny new prawnto_2 gem.

大多数事情看起来都很好,但我无法像以前那样加载图像.我的 PDF 视图中的代码是

Most things seem fine, but I can't get images to load as before. The code in my PDF view is

pdf.image open("#{RAILS_ROOT}/public/images/logo.png")

但我收到以下错误

uninitialized constant ActionView::CompiledTemplates::RAILS_ROOT

我意识到图像文件的位置会随着我使用资产而改变,并且图像不再存储在公共文件夹中.

I realise that the location of the image file will change as I'm using assets and the image is no longer stored in the public folder.

推荐答案

RAILS_ROOT 在全局命名空间中,所以必须使用

RAILS_ROOT is in the global namespace, so you have to use

::RAILS_ROOT

然而这已被弃用,所以更好地使用

However this is deprecated, so better use

::Rails.root

要为此附加路径,您可以使用它,它也适用于...erm...其他操作系统

To append a path to this, you can use this, which also works on ...erm... other operating systems

::Rails.root.join('public','images','logo.png')

这篇关于使用 prawnto_2 gem 加载图像时,RAILS_ROOT 不再有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆