Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6135591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:29:53+00:00 2026-05-23T17:29:53+00:00

Im using jQuery UI autocomplete. When i use a local variable as source it

  • 0

Im using jQuery UI autocomplete.

When i use a local variable as source it works.

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"},
    ];

But when i return the same source from another file, it doesnt work. I find the JSON object in firebug and it seems the same as my local variable json.

The code is the following:

$( ".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 );
    };

This is search.json.erb file:

[<% @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 %>]

What is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:29:53+00:00Added an answer on May 23, 2026 at 5:29 pm

    The problem was the missed callback. Autocomplete needs a callback param to function properly.
    The right search.json code is the following:

    <%= "#{params[ :callback ]}(" if params[:callback] -%>
    [<% @results.each_with_index do |r, i| %>
      <% if r.is_a? User %>
        {
          type: "Utente",
          label: <%= r.name.to_json.html_safe %>,
          url: "<%= user_url(r.id) %>"
        }
      <% elsif r.is_a? Question %>
        {
          type: "Domanda",
          label: <%= r.text.to_json.html_safe %>,
          url: "<%= question_url(r.slug) %>"
        }
      <% elsif r.is_a? Topic %>
        {
          type: "Argomento",
          label: <%= r.name.to_json.html_safe %>,
          url: "<%= topic_path(r.slug) %>"
        }
      <% end %>
      <%= "," unless i == @results.count - 1 %>
    <% end %>]
    <%= ")" if params[:callback] -%>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the jQuery autocomplete plugin to get a list of locations, which works
I am currently using the DevBridge jQuery Autocomplete plugin - it works fine as
I use Jorn Zaefferer's Autocomplete jQuery plugin which works great except for one thing
I'm using the jquery autocomplete UI widget, for example: var names = [John, Jack,
I am using jquery-ui's autocomplete in my project. It works great. Now I want
I am using this autocomplete Plugin working with Jquery: jQuery Autocomplete Mod You use
I am using Codeigniter and trying to use the jQuery autocomplete with it. I
I'm using JQuery UI Autocomplete to pull records from a caller database. This works
I'm using the jQuery UI Autocomplete widget. I need to use the selected text
Am using JQuery Autocomplete on my templete, but as i get the results the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.