2013-03-09 65 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