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 的组合,由以下响应生成。

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

问题是这对普通的jquery自动完成很有好处,但我正在寻找分类,我想知道如何创建带有标题和类别标签存在标签的对象,我会插入“用户" 或 "article" 取决于对象的类型。

这个想法是为 jquery categorize 提供以下输出

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

1 回答 1

1

我没有看到任何快速的方法来做到这一点,但就像这样

@titles.map { |title| { label: title.title, category: 'Products' } } +
@cus.map { |cus| { label: cus.first_name, category: 'People' } }
于 2013-03-09T02:39:50.700 回答