title: Rails 3.2.1 Jquery autocomplete
body: <p>I've been stuck for a few days on this.  I have been using railscasts #102 revised as a template. </p>
  
  <p>Ultimately I plan to have a search box in the top menu bar using ajax to query the name field of two seperate models which uses jquery-ui autocomplete and when clicked link directly to the show action for the selected search result.</p>
  
  <p>But I'm not even there yet, right now I'm just trying to get the autocomplete to work with a single model.  Here's what I have:</p>
  
  <p>-Opportunity model, searching off the NAME attribute</p>
  
  <p>-Search Controller  </p>
  
  <pre><code> def index  
       @opportunities = Opportunity.order(:name).where("name like ?", "#{params[:term]}")  
       render json: @opportunities.map(&amp;:name)  
   end  
  </code></pre>
  
  <p>Search.js.coffee  </p>
  
  <pre><code>jQuery -&gt;  
     $('#search').autocomplete  
     source: $('#search').data('autocomplete-source')  
  </code></pre>
  
  <p>Search view (/app/views/layouts/application.html.erb:  </p>
  
  <pre><code> &lt;%= text_field_tag 'search', nil, :class =&gt; 'search-query ui-autocomplete-input', :placeholder =&gt; 'Search', data: {autocomplete_source: search_path} %&gt;  
  </code></pre>
  
  <p>When I run this code, as I type letters into the search box (lets assume I type the letter B), I get:<br>
  Started GET "/search?term=b" for 127.0.0.1 at 2012-03-01 15:17:40 -0800<br>
  Processing by SearchController#index as JSON<br>
    Parameters: {"term"=>"b"}<br>
    Opportunity Load (0.3ms)  SELECT "opportunities".* FROM "opportunities" WHERE (name like   'b') ORDER BY opportunities.name, name<br>
  Completed 200 OK in 1ms (Views: 0.1ms | ActiveRecord: 0.3ms)  </p>
  
  <p>What I would now expect to happen is the autocomplete would use the B, select all Names in the model, and drop them down in the autocomplete, instead, I get nothing, nothing happens.</p>
  
  <p>If I adjust my view with the following:</p>
  
  <pre><code> &lt;%= text_field_tag 'search', nil, :class =&gt; 'search-query ui-autocomplete-input', :placeholder =&gt; 'Search', data: {autocomplete_source: @opportunities.map(&amp;:name)} %&gt;  
  </code></pre>
  
  <p>Then the autocomplete will work, but each page with the search bar will load all search terms to the client side.  With a potential of thousands of names I don't want to load all that data, I want the ajax call to be made live against the database and adjust the view accordingly.</p>
  
  <p>I'm new with rails so I'm sure I am making a dumb mistake here.  Any help would be so much appreciated.</p>
  
  <p>Thank you</p>