title: How to show search results dynamically in rails
body: <p>I'm working on search form in my rails application. Here is what I have now:</p>
  
  <p><strong>My view:</strong></p>
  
  <pre><code>&lt;%= form_tag search_suggestions_path, method: 'get', id: 'all-games-search' do %&gt;
    &lt;%= text_field_tag :games_search, (params[:games_search].present? ? params[:games_search] : nil),
                                     class: 'search-input', placeholder: 'Search games...', autocomplete: 'off' %&gt;
    &lt;ul id="results"&gt;&lt;/ul&gt;
    &lt;%= button_tag name: nil, class: 'submit-button' %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>def search_suggestions
      query = params[:games_search]
      results = if query.present?
                  response = []
                  Game.where('title ILIKE ?', query.to_s).each do |game|
                    obj = {id: game.id, title: "#{query}"}
                    response &lt;&lt; obj
                  end
                  response
                else
                  []
                end
      render json: results
    end
  </code></pre>
  
  <p>And <strong>js:</strong></p>
  
  <pre><code>$('#games_search').bind('change input paste', function() {
      var val = $(this).val();
      $.get('/search_suggestions?games_search=' + val, function (data) {
        $('#results').empty().show();
        $.each(data, function(index, results) {
          $('#results').append('&lt;li&gt;' + results.title + '&lt;/li&gt;').click(function () {
            location.href = '/games/' + results.id;
          });
        });
      });
    });
  </code></pre>
  
  <p>This code works, but it gives me the result, only if I enter the whole game's title. What I need, is to show search results, when the first few symbols of game's title are typed in the search input. Thanks ahead. </p>