2

I am attempting to use a remote service to populate an autocomplete field that I have in rails. It is supposed to query and return available employees by last name

I have an action in my controller called employeeAutocomplete which gathers data from an outside database:

class ServicesController < ApplicationController
 def employeeAutocomplete
  @banner = employeeSearch(params[:term])
  respond_to do |format|
    format.json { render :json => @banner.to_json }
  end
 end

In my routes.rb I have a placeholder route:

match '/banner/cheese' => 'services#employeeAutocomplete'

I can successfully browse to http://0.0.0.0:3000/banner/cheese.json?term=mac and receive an array such as the following with employee data:

[ {"LAST_NAME": "MacDougal", "FIRST_NAME": "Elaine", "TITLE": "Internet Technician"}, {"LAST_NAME": "MacCallum", "FIRST_NAME": "Harvey", "TITLE": "Systems Architect"} ]

However, this does not work with the autocomplete field. Here's the javascript for my view:

$("#service_employeeLast").autocomplete({
    source: "/banner/cheese.json"
});

I receive an error in the firebug console: I receive an error in the firebug console

I'm at wits end. I do not know what I'm doing wrong, I've tried two different autocomplete plugins and keep getting this same error jquery.js:8103

Help!

8
  • Can you show the whole text of the error that appears in the JavaScript console? Commented Mar 19, 2012 at 14:16
  • That is it. It says GET 0.0.0.0:3000/banner/cheese.json?term=mcc and then it says jquery.js:8103 Here is an image Commented Mar 19, 2012 at 14:25
  • I've even just modified the json it returns to fit the "label", "value" keys and that has made zero difference. I can still access the url through the web browser, but javascript acts like its inaccessible. If I paste the generated array directly into the source: it works! Something is wrong with the routing here. Why can't the backend js access the url like I can? Commented Mar 19, 2012 at 14:28
  • Can you do an AJAX request without the autocomplete stuff? So $.get('/banner/cheese.json', function (data, status, xhr) {console.log(data);}) Commented Mar 19, 2012 at 14:30
  • I added a term request so it would hopefully return something $.get('/banner/cheese.json?term=mcc', function (data, status, xhr) {console.log(data);}) and received the same message as the image above in the comment. Commented Mar 19, 2012 at 14:39

2 Answers 2

2

Believe it or not it was Google Chrome causing the issue. Apparently it dislikes localhost ajax calls. Here is the updated code working as intended:

services.js

$(function() {
 $( "#service_employeeLast" ).autocomplete({
    source: "/banner/fetch",
    minLength: 2,

 });
});

services_controller

def employeeAutocomplete
    @banner = employeeSearch(params[:term])
    @banner_hash = []
    @banner.each do |b|
        @banner_hash << { 
            :title => b["POSITION_TITLE"], 
            :label => [b["FIRST_NAME"], b["LAST_NAME"]].join(" "), 
            :value => b["LAST_NAME"], 
        }
    end
    render :json => @banner_hash
end

The key here also that your return value hash has a label and value key/value pair

Sign up to request clarification or add additional context in comments.

Comments

1

Maybe you encoded the data twice with render :json => @banner.to_json. It should be render :json => @banner, and the data will be encoded automatically.

1 Comment

Thanks, that makes sense. Didn't seem to make a difference though :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.