2013-03-09 61 views
0

我想實現使用rails分類的jquery自動完成。實現Json自動完成類別

我可以報告看起來像這樣

["Air Canada to appeal Quebec court ruling on Aveos","First Nations meeting with PM in jeopardy","Neanderthals, humans may have missed each other","New York state enacts tougher gun control measures","Ontario high school teachers drop next week's walkout plan","test","Jean-Sebastien","Serge","Dean","feng","Heather","Carmen","Frank"] 

他是user.firstname和article.titles的組合,並通過以下反應生成以下JSON響應。

render :json => (@titles.map(&:title) || []) | (@cus.map(&:first_name) || []) 

的問題是,這是良好的正常jQuery的自動完成,但我期待的分類和我想知道我怎麼可以創建一個標籤,其中,標題是何類別標籤將對象存在,我會根據對象的類型插入「用戶」或「文章」。

的想法是有jQuery的輸出如下分類

{ label: "annttop C13", category: "Products" }, 
    { label: "anders andersson", category: "People" }, 
    { label: "andreas andersson", category: "People" }, 
    { label: "andreas johnson", category: "People" } 

回答

1

我沒有看到任何快速的方法來做到這一點,但這樣的

@titles.map { |title| { label: title.title, category: 'Products' } } + 
@cus.map { |cus| { label: cus.first_name, category: 'People' } } 
+0

謝謝你,我希望這將工作對於jquery json的分類,但它是有道理的謝謝你 – Jseb 2013-03-09 02:42:59