output formated json with rails 3

后端 未结 3 1221
说谎
说谎 2021-02-06 18:10

I use rails 3.0.3

A javascript auto complete needs data like this

{
 query:\'Li\',
 suggestions:[\'Liberia\',\'Libyan Arab Jamahiriya\',\'Liechtenstein\'         


        
3条回答
  •  误落风尘
    2021-02-06 19:06

    If Adam's response won't work for you, this may do it (admittedly not the most elegant solution):

    {
        query:'<%= @query %>',
        suggestions: [<%= raw @customers.map{|c| "'#{c.firstname} #{c.lastname}'" }.join(", ") %>],
        data: [<%= raw @customers.map{|c| "'#{c.id}'" }.join(", ") %>]
    }
    

提交回复
热议问题