I’m going off Railscasts 102.
My current issue is that my search form is already populated with data when the page loads.
I have one model, listing, and 5 columns on that model: website, url, comments, doctor, and date. In this case I am just worrying about website.
My listings.js.coffee is
jQuery ->
$('#search').autocomplete
source: $('#search').data('autocomplete-source')
And my index.html.erb file (the important part) is
<%= form_tag '/listings', :method => :get do %>
<%= text_field_tag :search, data: {autocomplete_source: Listing.order(:website).map(&:website)} %>
<%= submit_tag 'Search', :class => 'echo-search-tag', :name => nil %>
<% end %>
When I load the page, the search form is already populated with
{:data=>{:autocomplete_source=>[“Doctors4US”, “How To Get Better”, “Rafael’s Epic Doctor Site”, “Testing Admin”, “WebMD”]}}
Obviously this shouldn’t be the case. The JS console is giving me an error of:
Uncaught TypeError: Property ‘source’ of object # is not a function
I saw another Stack Overflow post in regards to this, I tried that solution but it didn’t make a difference.
Note: when I tried test data for the source in listings.js.coffee, like [‘foo’, ‘fore’, ‘food’], the autocomplete was working correctly.
Also note that the search function is working correctly aside from autocomplete.
Any input is appreciated 🙂
Ok the issue was that the text_field_tag takes 2 arguments, and that is why the search form was being prepopulated with the data.
So I just added nil for the 2nd argument
And voila! Working.
Now trying to figure out how to add multiple columns to Listing.order and .map (ie, having not only :website, but :comments, :doctor, and :url). Input appreciated!