开发者

Help with rails json and jquery

Im trying to use this jquery ui widget for autocompleting a textbox in my app. I have created an index.json.erb file in开发者_如何学运维side my views/university folder

here's my current (piece of crap) code

json='['
<% @universities.each do |u| %>
json+='{ "id": "#{@u.name}" , "label":"#{@u.name}" , "value": "#{@u.name}"},'
<% end %>
json+=']'
json

Needless to say it doesnt work at all... its outputting the whole thing.. not my created json file...... I cant find a builder alternative for creating json. (builder helps creating xml).

please help!


Replace it with this:

<%=
'[' + (
   @universities.map do |u|
     %Q[{ "id": "#{u.name}" , "label": "#{u.name}" , "value": "#{u.name}" }]
   end.join(',')
) + ']'
%>

Edit: To wrap the label in <em>, replace the line

%Q[{ "id": "#{u.name}" , "label": "#{u.name}" , "value": "#{u.name}" }]

with

%Q[{ "id": "#{u.name}" , "label": "<em>#{u.name}</em>" , "value": "#{u.name}" }]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜