output formated json with rails 3

后端 未结 3 1222
说谎
说谎 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:13

    I've seen something like this in a .erb:

    <%= raw
      {
        :query       => @query,
        :suggestions => @customers.map{|c| "#{c.firstname} #{c.lastname}" },
        :data        => @customers
      }.to_json
    %>
    

    If thinking of preparing data to be consumed by other programs as presentation logic, this might make sense to you.

    FWIW I like it.

提交回复
热议问题