我是rails和jbuilder的新手,所以我不确定这是否可行。我正在使用这个自动完成 jquery 插件http://www.devbridge.com/sourcery/components/jquery-autocomplete/并且它期望的响应格式为
{
query:'Li',
suggestions:['Liberia', 'Libyan Arab Jamahiriya', 'Liechtenstein', 'Lithuania'],
data:['LR', 'LY', 'LI', 'LT']
}
当我尝试使用 jbuilder 返回 json 时,我得到了一个 json 对象,其中的键和值都用引号括起来
{"query":"Comp","suggestions":"['Test Company','Test Company 2','Test company','tester chester','before create test']","data":"['1','2','3','4','5']"}
看来这导致插件无法正常工作,那么有没有办法可以从返回的 json 中删除双引号?这是我的 jbuilder 文件的样子。
suggestions = ""
data = ""
@companies.each do |company|
suggestions += "'" + company.name + "',"
data += "'" + company.id.to_s + "',"
end
json.query @query
json.suggestions "[" + suggestions[0...-1] + "]"
json.data "[" + data[0...-1] + "]"
谢谢!