1

I'm using AssetPack to handle public assets on my Sinatra app. Because the app work as a embeddeable ad on sites, I need to declare css assets route absolute. This is my current code:

require 'rubygems'
require 'sinatra'
require 'rack'
require 'sinatra/assetpack'

class Ads < Sinatra::Application

  assets {
    css :mybanner, "http://#{request.host_with_port}/css/styling.css", [
      "http://#{request.host_with_port}/css/styling.css"
    ]
  }

The problem is that when calling request.host_with_port I'm getting the following error

NameError: undefined local variable or method `request' for #    <Sinatra::AssetPack::Options:0x007fc1f88b0a80>

I'm not sure why request is not working. Any idea?

phillbaker
  • 1,518
  • 11
  • 22
Martin
  • 11,216
  • 23
  • 83
  • 140
  • It isn't working because you're calling it from outside the Sinatra app scope. If you pay attention you'd see that it is called from the `Sinatra::AssetPack::Options` instance. – Samy Dindane Aug 03 '12 at 10:38

1 Answers1

1

As far as I understood, AssetPack builds the assets at server start.
The request object isn't available at that time, obviously.

Here's what I'd suggest:

class Ads < Sinatra::Application
  host_with_port = ENV['HOST_WITH_PORT']

  assets {
    css :mybanner, "http://#{host_with_port}/css/styling.css", [
      "http://#{host_with_port}/css/styling.css"
    ]
  }
end

You'd need to set a HOST_WITH_PORT environment variable, but you'll do it only once for each site.

Samy Dindane
  • 17,900
  • 3
  • 40
  • 50