I am new to ROR, I have some seed data stored in my database table and a YAML file. I have been loading the yaml file and converting it into a JSON which I parsed and displayed to the client.
我是ROR的新手,我有一些种子数据存储在我的数据库表和YAML文件中。我一直在加载yaml文件并将其转换为JSON,我将其解析并显示给客户端。
Something like this.
像这样的东西。
controller.rb
def template_library
@template_library_all= YAML::load(File.open('./db/seeds/template_library.yml'))
end
In my view I did
在我看来,我做到了
reports.html.slim
javascript:
var templateLibraryJSON = #{@template_library_all.to_json.html_safe};
So now I want to use the model to get the data from the database and parse it into JSON, instead of using a static file.
所以现在我想使用该模型从数据库中获取数据并将其解析为JSON,而不是使用静态文件。
What I have done so far.
到目前为止我做了什么。
def query_library
@template_library_JSON = TemplateLibrary.all.map { |i| ['file_name:' , [i.file_name]]}
end
in my view
在我看来
javascript:
var templateJSON = #{@template_library_JSON.to_json.html_safe};
this returns me a JSON which looks like a JSON array.
这会返回一个看起来像JSON数组的JSON。
[["file_name:", ["daily_data_count_report"]]]
Do I have to construct the JSON object ?
我是否必须构造JSON对象?
1 个解决方案
#1
1
It looks like your #map call returns an array of arrays, not a hash. Try this instead:
看起来你的#map调用返回一个数组数组,而不是一个哈希。试试这个:
@template_library_JSON = TemplateLibrary.all.map { |i| { :file_name => i.file_name } }
@template_library_JSON = TemplateLibrary.all.map {| i | {:file_name => i.file_name}}
#1
1
It looks like your #map call returns an array of arrays, not a hash. Try this instead:
看起来你的#map调用返回一个数组数组,而不是一个哈希。试试这个:
@template_library_JSON = TemplateLibrary.all.map { |i| { :file_name => i.file_name } }
@template_library_JSON = TemplateLibrary.all.map {| i | {:file_name => i.file_name}}