title: How to use a route helper method from a file in the lib directory?
body: <p>I need to use the <code>root_url</code> method from a method defined in a file in the <code>lib</code> folder. Is that possible?</p>
  
  <p>I tried including this line in my class:</p>
  
  <pre><code>include Rails.application.routes.url_helpers
  </code></pre>
  
  <p>but this gives me the error</p>
  
  <blockquote>
    <p>Missing host to link to! Please provide :host parameter or set default_url_options[:host]</p>
  </blockquote>
  
  <hr>
  
  <p><strong>Edit:</strong> I found out that it works if I first initialize the routes:</p>
  
  <pre><code>def initialize_routes
    if Rails.env.development? || Rails.env.test?
      Rails.application.routes.default_url_options[:host] = 'localhost:3000' 
    elsif Rails.env.production?
      Rails.application.routes.default_url_options[:host] = 'example.com'
    end
  end
  </code></pre>
  
  <p>Is there a better way to accomplish this? Maybe setting the routes in a config file?</p>