Background: I’ve been doing RoR for about a year now, and am fairly comfortable with it, however, I know next to nothing about Javascript.
I’ve been playing around with some jquery autocomplete stuff in my rails app. I pretty much had a version working, but needed some tokenized fields too for a one to many relationship.
Right on cue – good old Ryan Bates does a railscast on it. So I start following the instructions.
Got a little bit nervous when I had to start installing ‘jquery-rails’ gem (I’d already installed jrails to get the other stuff working).
As suspected, it broke some stuff but I managed to get that working again.
Anyway, I got most of the way through the tutorial, and everything was going fine – I’ve got the tokenizer script to find the correct input field and it acts as expected. I’ve tested the json link too – that sends back all the right stuff.
However when I start typing in the text field – nothing happens, and when I view the console window it comes back with:
Uncaught TypeError: Cannot call method 'replace' of undefined
jQuery.jQuery.extend._Deferred.deferred.resolveWith
done
jQuery.ajaxTransport.send.callback
I can make guesses as to why this is going wrong – but any expert advice would be greatly appreciated.
(I should also add – I’m using formtastic too)
Thanks in advance.
Ok, finally figured it out.
It turns out that my author’s name column is not
namebut ratherauthor. So I needed to make a change inside thejs.coffeescript to override that default search ofname.The line you need to use is:
My whole book.js.coffee file now looks like this:
This actually fixed the error
Uncaught TypeError: Cannot call method 'replace' of undefinedOf course, if you do use a different column name you will want to also edit the functions in the author.rb file to reflect that:
Edit
Another thing I had to do for the fieldsto be prepopulated with the existing authors was change this:
To this:
And then they would show up.
Hope this helps you.