[英]How do call a javascript file from another Rails project? ActionController::RoutingError (No route matches [GET] “/assets/trackmetrics.js”)
I am currently testing out my API which has a JS script in the assets folder -- trackmetrics.js. 我目前正在测试我的API,该API在Assets文件夹(trackmetrics.js)中具有JS脚本。 I created another rails app for testing purposes, and I am trying to call trackmetrics.js file from another url localhost:3003
我创建了另一个Rails应用进行测试,并且试图从另一个URL localhost:3003调用trackmetrics.js文件
Trackmetrics app is on localhost:3000 while Simple app is on localhost:3003 Trackmetrics应用位于localhost:3000上,而简单应用位于localhost:3003上
My Simple app has the following code in the view: 我的简单应用程序在视图中具有以下代码:
<h1>Welcome#index</h1>
<p>Find me in app/views/welcome/index.html.erb</p>
<%= javascript_include_tag "//localhost:3000/assets/trackmetrics.js" %>
<script>
Trackmetrics.setAuthToken('######################################');
Trackmetrics.send(“page_view”,{test: true});
</script>
Is there a way for Simple App to GET the trackmetrics.js file that's in Trackmetrics on localhost:3000? Simple App是否可以获取localhost:3000上Trackmetrics中的trackmetrics.js文件? I have both applications running and I ran into the following error when testing it:
我同时运行了两个应用程序,并且在测试时遇到以下错误:
Started GET "/assets/trackmetrics.js" for 127.0.0.1 at 2014-07-22 21:59:38 -0400
ActionController::RoutingError (No route matches [GET] "/assets/trackmetrics.js"):
My Trackmetrics.js file looks like this: 我的Trackmetrics.js文件如下所示:
var Trackmetrics = {
authToken: null,
setAuthToken: function(auth_token) {
this.authToken = auth_token;
},
send: function(name, data) {
var _bm_request = new XMLHttpRequest();
_bm_request.open("POST", "http://localhost:3000/events.json", true);
_bm_request.setRequestHeader('Content-Type', 'application/json');
_bm_request.onreadystatechange = function() {
// this function runs when the Ajax request changes state.
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
};
_bm_request.send(JSON.stringify({auth_token: this.authToken, name: name, data: data}));
}
}
Also, I have tried setting my config.serve_static_assets = false
and did 另外,我尝试设置
config.serve_static_assets = false
并做了
rake assets:precompile...and I am still running into the error.
This is not an issue of one app calling another apps javascript. 这不是一个应用程序调用另一个应用程序javascript的问题。 It looks like the app on :3000 doesn't recognize the route.
似乎:3000上的应用无法识别路线。 Get it working on the first app first!
首先让它在第一个应用程序上运行!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.