为类别实现Json自动完成

时间:2013-03-09 02:26:40

标签: jquery ruby-on-rails ruby json ruby-on-rails-3

我正在尝试使用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的组合,由以下回复生成。

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

问题是这对于正常的jquery自动完成是有好处的,但是我正在寻找分类,我想知道如何创建具有标题的对象,其中标题将存在并且我将根据对象的类型插入“user”或“article”。

我们的想法是为jquery分类

提供以下输出
  { label: "annttop C13", category: "Products" },
  { label: "anders andersson", category: "People" },
  { label: "andreas andersson", category: "People" },
  { label: "andreas johnson", category: "People" }

1 个答案:

答案 0 :(得分:1)

我没有看到任何快速的方法,但像这样

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