[英]Dynamically set value in Rails javascript asset based on Rails environment
I have a js file used for Recurly: 我有一个用于递归的js文件:
(function() {
// Required
Recurly.config({
subdomain: 'MYSUBDOMAIN',
currency: 'USD'
});
})();
I'd like to be able to switch the subdomain attribute based on my Rails environment but keep this javascript available to the asset pipeline. 我希望能够根据我的Rails环境切换subdomain属性,但保持此javascript可用于资产管道。 Is this possible within asset pipeline or what is the best way to achieve the equivalent functionality?
在资产流水线内这是否可能,或者实现等效功能的最佳方法是什么?
It's a disappointingly hacky solution but you could set a global variable in the <head>
of your document before your asset pipelined JS is included and then use that variable later, eg: 这是一个令人失望的解决方案,但是您可以在包含资产管道JS之前在文档的
<head>
中设置全局变量,然后在以后使用该变量,例如:
In your application.html.erb
: 在您的
application.html.erb
:
<head>
...
<%= javascript_tag "var subdomain = '#{request.subdomains.join(".")}';" %>
<%= javascript_include_tag "application" %>
...
</head>
And in your JS: 在您的JS中:
(function() {
// Required
Recurly.config({
subdomain: subdomain,
currency: 'USD'
});
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.