0
我試圖在rails應用程序上有一個自動完成文本字段。我的問題涉及自定義數據源。將數組傳遞給json格式,用於自動完成jquery
以下是我得到的數據: 我查找聯繫人姓名,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數據= [[" AL Tohtori ",279,"別人"],["阿巴特卡琳",296,"別人"]
與
@result.map {|r| {:label => r[0], :value => r[1], :category => r[2] } }
我似乎無法得到它。
有什麼建議嗎?
謝謝!