Rails to_json 问题
我的 Rails 3 应用程序中的 to_json 方法有一个奇怪的问题。 (至少我认为这与 to_json 有关)
在我的控制器中,我获取存储在数据库中的所有库的列表
@libraries = Library.where( "latitude IS NOT NULL AND longitude IS NOT NULL" )
,然后我创建一个包含上面库信息的 json 文件。
my_file = File.new("public/javascripts/libraries.json", "w")
my_file.write "var libs = {'libraries' : "
my_file.write @libraries.to_json( :only => [ :id, :name, :address, :latitude, :longitude ])
my_file.write "};"
然后在我看来,我在 Google 地图上显示每个库对象。在视图文件中,我通过将libraries.json 文件加载为javascript 文件来读取json 文件。
现在的问题是,库对象有时会显示在 Google 地图上,但不是一直显示,通过 Firebug,我能够确定有时 JSON 文件中包含的“libs”变量是“未定义”的。
这让我认为文件还没有完全写入,或者文件中的数据还没有完全加载。但我不太确定它是什么。
有谁知道这可能是什么原因造成的?
I have a strange issue with to_json method in my Rails 3 app. (well at least I think it is to do with to_json)
In my controller, I am getting the list of all the libraries stored in the DB
@libraries = Library.where( "latitude IS NOT NULL AND longitude IS NOT NULL" )
And then I create a json file that contains the library information above.
my_file = File.new("public/javascripts/libraries.json", "w")
my_file.write "var libs = {'libraries' : "
my_file.write @libraries.to_json( :only => [ :id, :name, :address, :latitude, :longitude ])
my_file.write "};"
Then in my view, I display each library object on Google Map. In the view file, I am reading the json file by loading the libraries.json file as a javascript file.
Now the problem is that the library objects are displayed on Google map SOMETIMES, but not all the time, and through Firebug, I was able to determine that sometimes the "libs" variable, that is contained in the JSON file is "undefined".
This makes me think that the file has not been completely being written, or the data in the file has not been completely been loaded. But I am not too sure what it is.
Does anyone have an idea what could be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定为什么会出现这个问题,但我认为这与您正在创建的文件有关。
我建议您
简单地将 json 数据返回到视图
,然后您可以使用 javascript 或 jquery 或任何其他框架在客户端处理它。这样你也可以通过在 Firebug 中查看来确保数据是正确的。
或者
无需将其设为文件,只需
将 json 数据保存在变量中
,然后您就可以在视图中访问该变量。当您无需创建文件就可以创建文件时,为什么要费心去创建文件呢?
原因:
我这么说是因为每次您都在创建一个新文件,而您在当前 http 调用之前创建的文件都消失了并且没有用处。
I am not quite sure why the problem is there, but I think it is related to the file that you are creating.
I would suggest you to
simply return the json data to the view
and then you can process it at the client side using javascript or jquery or any other framework.This way you can also ensure that the data is correct by looking at it in Firebug.
OR
Instead of making it a file, just
hold the json data in a variable
and you can then access the variable in the view.Why go for the trouble of creating a file, when you can do it without it.
Reasons:
I am saying so because each time, you are creating a new file, and the files that you created before the current http call are gone and of no use.
是的,这可能是由于您将其写入文件而不是仅将其包含在标签中而引起的。因为(有时)可能发生的情况是服务器收到请求,写入 json 文件,然后渲染视图并将该视图发送到客户端。然后,客户端被告知加载libraries.json 文件,从而导致向服务器发出另一个请求来检索该文件。但是,如果由于另一个客户端同时发送了请求而导致该文件在该时刻被重写怎么办?它会失败。
您还可以通过其他几种方法来解决此问题。首先,可能最快的启动和运行是将其包含在控制器和视图中,而不是写入文件:
但是更好的解决方案可能是安排一些作业或任务,以按计划间隔从数据库加载库,并且也许每天一次写入 json 文件,因为我想实际上没有必要一直从数据库加载这些文件,除非它们从一分钟到下一分钟发生变化。
Yes, it is probably caused by the fact that you write it to a file instead of just including it in a tag. Because what probably happens (sometimes) is that the server gets the request, writes the json file, then renders a view and sending that view to the client. The client is then told to load the libraries.json file resulting in another request to the server to retrieve that file. But what if that file in that exact moment is being rewritten because another client has sent a request at the same time? It will fail.
There are a few other ways you could go about this. First and probably the quickest to get up and running is to include this in your controller and view instead of writing to a file:
But a better solution would probably be to schedule some job or task to load the library from the database at schedule intervals and write to a json file perhaps once a day because I guess it is not really necessary to load these from the database all the time unless they change from one minute to the next.