我正在尝试在rails应用程序上拥有自动填充文本字段。我的问题涉及自定义数据源。
以下是我获取数据的方式: 我查找了联系人姓名,ID并添加了一个类别,以便使用this example
在自动填充中显示该类别如果用户是朋友,则该类别是联系人,否则该类别是其他人
@result = Array.new
# find the current user id when its viewed as a contact
@current_contact = Contact.find_by_user_id(current_user).id
#go through all the contacts
Contact.order('name ASC').each do |a|
#if the current contact is a friend of the user
if Relationship.find_by_user_id_and_contact_id(current_user, a)
#add it to the array with the 'contacts' category
@result << [a.name, a.id, 'contacts']
else
#if its not a friend, and its not himself, add it to the array with the 'others' category
unless @current_contact == a.id
@result << [a.name.to_s,a.id, 'others']
end
end
end
如何输出此格式,以便自动完成功能可以将其作为数据源?
该示例显示了我正在寻找的格式,它似乎是json格式
var data = [
{ label: "andreas andersson", category: "People" },
{ label: "andreas johnson", category: "People" }
];
但我似乎无法将输出转换为那个输出。我试过了
@result.to_json
var data = [[“AL Tohtori”,279,“others”],[“Abat Karine”,296,“others”]]
与
@result.map {|r| {:label => r[0], :value => r[1], :category => r[2] } }
我似乎也无法拥有它。
有什么建议吗?
谢谢!