Full Asset URL in a Rails .js.erb file

血红的双手。 提交于 2019-12-09 12:16:13

问题


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?


回答1:


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

# development.rb
config.action_controller.asset_host = "http://localhost:3000"


来源:https://stackoverflow.com/questions/10186523/full-asset-url-in-a-rails-js-erb-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!