RAILS_ROOT not longer valid when loading images with the prawnto_2 gem

廉价感情. 提交于 2019-12-12 09:36:09

问题


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

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.

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")

but I get the following error

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.


回答1:


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

::RAILS_ROOT

However this is deprecated, so better use

::Rails.root

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')


来源:https://stackoverflow.com/questions/9192795/rails-root-not-longer-valid-when-loading-images-with-the-prawnto-2-gem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!