ruby-on-railsprawnprawnto

RAILS_ROOT not longer valid when loading images with the prawnto_2 gem


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.


Solution

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