Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top