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?
1 Answer 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.
4 Comments
jQuery setupAutocomplete in console, though, I get an error about an unexpected identifier.