Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full Asset URL in a Rails .js.erb file

I have a bookmarklet that can be loaded on any website.

It works like this;

  • loads http://my.example.com/assets/bookmarklet.js
  • includes http://my.example.com/assets/bookmarklet.css

The css file is added to the dom via bookmarklet.js, so it needs to know where this is.

My code is as follows;

// assets/javascripts/bookmarklet.js.erb

var config = {
  stylesheetUrl: '<%= asset_path("bookmarklet.css", :only_path => false) %>'
}

But no matter what I try this renders as

var config = {
  stylesheetUrl: '/assets/bookmarklet.css'
}

I need the asset_path to return a full url. But asset_url doesn't exist, and I can't find an option to asset_path that will prepend the current domain.

Any solution?

like image 574
Matthew Rudy Avatar asked Apr 17 '12 06:04

Matthew Rudy


1 Answers

You need to set an explicit asset host for each environment. For example:

# development.rb
config.action_controller.asset_host = "http://localhost:3000"
like image 85
Thilo Avatar answered Sep 19 '22 10:09

Thilo