Я использую автозаполнение пользовательского интерфейса jQuery.
Когда я использую локальную переменную в качестве источника, она работает.
var json = [
{type: "Utente",label: "Luca XXXX",url: "http://lvh.me:3000/users/4dde465add53e04e5c000001"},
{type: "Domanda",label: "Luca asdas adsfdsfdsf sdsd",url: "http://lvh.me:3000/questions/luca-asdas-adsfdsfdsf-sdsd"},
];
Но когда я возвращаю тот же источник из другого файла, он не работает.Я нахожу объект JSON в Firebug, и он выглядит так же, как моя локальная переменная json.
Код следующий:
$( ".query-input" ).autocomplete({
minLength: 3,
source: "/search.json",
select: function( event, ui ) { window.location = ui.item.url }
})
.data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" )
.data( "item.autocomplete", item )
.append( "<a href=" + item.url + ">"+ item.label +"</a><span>" + item.type + "</span>" )
.appendTo( ul );
};
Это файл search.json.erb:
[<% @results.each do |r| %>
<% if r.is_a? User %>
{type: "Utente",label: <%= r.name.to_json.html_safe %>,url: <%= user_url(r.id).to_json.html_safe %>},
<% elsif r.is_a? Question %>
{type: "Domanda",label: <%= r.text.to_json.html_safe %>,url: <%= question_url(r.slug).to_json.html_safe %>},
<% elsif r.is_a? Topic %>
{type: "Argomento",label: <%= r.name.to_json.html_safe %>,url: <%= topic_path(r.id).to_json.html_safe %>},
<% end %>
<% end %>]
Что не так?