Ruby 数组到 JSON 和 Rails JSON 渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4253238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby array to JSON and Rails JSON rendering
提问by Mithun Sreedharan
I have a Ruby array, how can i render this as a JSON view in Rails 3.0?
我有一个 Ruby 数组,如何在 Rails 3.0 中将其呈现为 JSON 视图?
My Controller method is
我的控制器方法是
def autocomplete
@question = Question.all
end
回答by Pierre
If the autocomplete action is only rendering JSON, you could simplify re5et's solution to:
如果自动完成操作仅呈现 JSON,您可以将 re5et 的解决方案简化为:
def autocomplete
questions = Question.all
render :json => questions
end
(note that I pluralized 'question' to reflect that it is an array and removed the @ symbol - a local variable suffices since you're probably only using it to render inline JSON)
(请注意,我将 'question' 复数化以反映它是一个数组并删除了 @ 符号 - 一个局部变量就足够了,因为您可能只使用它来呈现内联 JSON)
As kind of an addendum, because I suspect people might land on this page looking for a solution to the jquery ui autocomplete plugin, rendering the array questionto JSON won't work. The plugin requires a specific format as described here:
作为一种附录,因为我怀疑人们可能会登陆此页面寻找 jquery ui 自动完成插件的解决方案,将数组呈现question为 JSON 将不起作用。该插件需要此处描述的特定格式:
The local data can be a simple Array of Strings, or it contains Objects for each item in the array, with either a label or value property or both. The label property is displayed in the suggestion menu. The value will be inserted into the input element after the user selected something from the menu. If just one property is specified, it will be used for both, eg. if you provide only value-properties, the value will also be used as the label.
When a String is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL. The data itself can be in the same format as the local data described above.
本地数据可以是一个简单的字符串数组,或者它包含数组中每个项目的对象,带有标签或值属性或两者兼而有之。label 属性显示在建议菜单中。在用户从菜单中选择某些内容后,该值将被插入到 input 元素中。如果只指定了一个属性,它将用于两者,例如。如果您仅提供 value-properties,则该值也将用作标签。
当使用字符串时,自动完成插件期望该字符串指向将返回 JSON 数据的 URL 资源。它可以在同一台主机上,也可以在不同的主机上(必须提供 JSONP)。请求参数“term”被添加到该 URL。数据本身可以采用与上述本地数据相同的格式。
In other words, your json should look something like this (in its simplest form):
换句话说,你的 json 应该是这样的(最简单的形式):
[{'value': "Option1"},{'value': "Option2"},{'value': "etc"}]
You could accomplish this in ruby like so:
您可以像这样在 ruby 中完成此操作:
def autocomplete
questions = Question.all # <- not too useful
questions.map! {|question| {:value => question.content}}
render :json => questions
end
I haven't tested this since I don't have my dev box handy. I will confirm in a few hours when I do.
我没有测试过这个,因为我没有我的开发箱。我会在几个小时内确认。
UPDATE:yup, that works!
更新:是的,这有效!
UPDATE 2:
更新 2:
The new rails way to do this (added in rails 3.1) would be:
执行此操作的新 rails 方法(在 rails 3.1 中添加)将是:
class MyController < ApplicationController
respond_to :json
# ...
def autocomplete
questions = Question.all # <- not too useful
questions.map! {|question| {value: question.content}}
respond_with(questions)
end
end
回答by re5et
def autocomplete
@question = Question.all
respond_to do |format|
format.json { render :json => @question }
end
end

