views:

123

answers:

1

Im trying to use this jquery ui widget for autocompleting a textbox in my app. I have created an index.json.erb file inside 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!

A: 

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}" }]
Adrian
thanks...! Sorry to ask for a little more but you think there is a way I could wrap the label with the part thats been input by the user with an <em>user_input</em> and leave the rest without it?? I know its probably kind of crazy but just in case
NachoF
NachoF: Sorry, I don't know how to do that, but see my edit at the bottom of my answer for how to wrap the whole thing.
Adrian