简体   繁体   中英

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. I created another rails app for testing purposes, and I am trying to call trackmetrics.js file from another url localhost:3003

Trackmetrics app is on localhost:3000 while Simple app is on 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? 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:

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

rake assets:precompile...and I am still running into the error.

This is not an issue of one app calling another apps javascript. It looks like the app on :3000 doesn't recognize the route. Get it working on the first app first!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM