RAILS_ROOT not longer valid when loading images wi

2019-04-21 21:28发布

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条回答
在下西门庆
2楼-- · 2019-04-21 21:31

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')
查看更多
登录 后发表回答