Search code examples
ruby-on-railspdfurl-routingprawn

How to use rails routes in external classes?


I'm using prawn to generate pdfs, set up ala https://github.com/prawnpdf/prawn/wiki

I'd like to access my routes so I can generate links in my pdfs, but now I'm not in a template like I used to do with prawnto, so I don't have access to the named routes.

class MyPdf < Prawn::Document
  def to_pdf
    text root_path
  end
end

How can I include my named routes?


Solution

  • You should include ActionController::UrlWriter

    And if you are trying to use named routes from outside Rails environment, then you should load the environment, but this loading will be slow

    #If Rails environment is not loaded, be sure you include config/boot.rb correctly
    require File.dirname(__FILE__) + 'root_to_your_boot_rb_file'
    require RAILS_ROOT + '/config/environment'
    
    include ActionController::UrlWriter