0

I'm trying to implement autocomplete functionality available in Jquery-ui, as part of this railscast. Ideally, I should end up with something that looks like this github repo.

But I'm having trouble with a coffeescript file that's related a resource named "contacts."

#app/assets/javascripts/contacts.js.coffee
jQuery ->
 $('#search').autocomplete
 source: "/search_suggestions"

When I run this code in the browser's console, it works. But when it's in the coffeescript file it doesn't. The file loads in the browser. It just doesn't run the code.

What am I doing wrong?

asked Mar 18, 2013 at 1:08

1 Answer 1

1

My guess is somehow you are loading this file after the document ready event has fired. You can confirm/deny this guess by doing this:

setupAutocomplete = ->
 $('#search').autocomplete
 source: "/search_suggestions"
jQuery setupAutocomplete

Then once the page is loaded, open the console and manually call the setupAutocomplete function. If things then work, it means your document ready callback never fired for this, presumably because it happened before this code was loaded. So look into that.

answered Mar 18, 2013 at 3:06
Sign up to request clarification or add additional context in comments.

4 Comments

Well, that explanation definitely makes sense. When I run jQuery setupAutocomplete in console, though, I get an error about an unexpected identifier.
OK, so do you actually have jQuery loaded? Did you set that up properly as a dependency?
Yes. I have jquery and jquery-ui loaded. I can all the autocomplete method from Jquery-ui via the console, which wouldn't be possible if I didn't have it setup. (Unless there's something I'm missing here. I'm definitely a jquery novice.)
Hey do you think it might be helpful for you to actually post the stack trace from your unexpected identifier error? That indicates some basic syntax error that should be straightforward to fix when clairvoyance isn't required.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.