简体   繁体   English

将JavaScript从Rails控制器加载到javascript文件中的d3 .defer()函数

[英]load JSON from rails controller to d3 .defer() function within javascript file

I have a rails app that uses d3 to produce a globe with cities on it. 我有一个使用D3制作带有城市的地球仪的Rails应用程序。 Currently the data for the cities are in a static json file called places.json. 当前,城市的数据位于一个名为places.json的静态json文件中。 The places.json file is then being rendered to the rest of the d3 code within assets/javascripts through this snippet 然后,通过此代码段将places.json文件呈现到资产/ javascript中的其余d3代码

 queue()
.defer(d3.json, "/assets/world-110m.json")
.defer(d3.json, "/assets/places.json")
.await(ready);

I would like to replace the "/assets/places.json" file with a controller that rendors dynamic json. 我想将控制器替换为“ /assets/places.json”文件,该控制器会重载动态json。 I currently have the controller in operation and I have a post request that can get the json to the javascripts file below. 我目前正在使用控制器,并且有一个发布请求,可以将json获取到下面的javascripts文件中。

$.post( "/teams/to_geo_json", function( data ) {
   alert( JSON.stringify(data) );
});

where /teams/to_geo_json is the path to the controller. /teams/to_geo_json是控制器的路径。 However when I put the controller path into the .defer(d3.json, "/assets/places.json") it does not work and I have no idea how to get the $.post("/teams/to_geo_json")... call to work with the rest of the d3.js code. 但是,当我将控制器路径放入.defer(d3.json, "/assets/places.json")它不起作用,我也不知道如何获取$.post("/teams/to_geo_json")...调用以使用d3.js其余代码。

As it turns out I was getting a 404 error in the defer. 事实证明,我在延期中遇到404错误。 In my routes I had a 在我的路线上,我有一个

   post "/teams/to_geo_json" => "teams#to_geo_json"

In the console, it was asking for a get method from defer(). 在控制台中,它要求从defer()获取get方法。 I changed the route above to this: get "/teams/to_geo_json" => "teams#to_geo_json" 我将上面的路线更改为:get“ / teams / to_geo_json” =>“ teams#to_geo_json”

Once I changed that I still had issues where localhost:3000/teams/to_geo_json was going to localhost:3000/teams/show/id instead and it thought to_geo_json was the id which of course it could not find. 更改之后,我仍然遇到问题localhost:3000/teams/to_geo_json转到localhost:3000/teams/show/id ,它认为to_geo_json是它当然找不到的id。 So it errored out. 所以它出错了。 To fix this I put 为了解决这个问题,我把

 get "/teams/to_geo_json" => "teams#to_geo_json"

above 以上

resources :teams, :only => [:index, :show] do
   resources :users, :path => :teammates, :as => :teammates, :controller =>    "teammates"
 end

in my routes.rb folder 在我的routes.rb文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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