本文介绍了Rails .js.erb文件中的完整资产URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可以在任何网站上加载的小书签.
I have a bookmarklet that can be loaded on any website.
它是这样的
- 加载
http://my.example.com/assets/bookmarklet.js
- 包括
http://my.example.com/assets/bookmarklet.css
- loads
http://my.example.com/assets/bookmarklet.js
- includes
http://my.example.com/assets/bookmarklet.css
css文件是通过bookmarklet.js
添加到dom的,因此它需要知道它在哪里.
The css file is added to the dom via bookmarklet.js
, so it needs to know where this is.
我的代码如下;
// 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'
}
我需要asset_path
返回完整的URL.但是asset_url
不存在,并且我找不到asset_path
的选项来代替当前域.
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.
有解决方案吗?
推荐答案
您需要为每个环境设置一个明确的资产宿主.例如:
You need to set an explicit asset host for each environment. For example:
# development.rb
config.action_controller.asset_host = "http://localhost:3000"
这篇关于Rails .js.erb文件中的完整资产URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!