Live Search in Rails 5.0.3

Viewed 1193

I am working to get a live search up and running, but am having trouble. The search feature works fine when clicking the search button. I have looked at every stack question with live search. Here are two of the external links (reputation prohibits more) that I have tried:

http://railscasts.com/episodes/240-search-sort-paginate-with-ajax

http://aymeric.gaurat.net/2010/how-to-develop-live-search-textbox-in-ruby-on-rails/

My search functionality as it stands is listed below:

index.html.erb

<%= form_tag(products_path, method: :get) do %>
  <%= text_field_tag :term, params[:term] %>
  <%= submit_tag 'Search', name: nil %>
<% end %>

<div id="grid-display">
  <%= render "products" %>
  <%= paginate @products %>
</div>

products_controller.rb

@products = Product.search(params[:term]).page(params[:page]).per(9)

product.rb

def self.search(term)
  if term
    where('product_name LIKE ?', "%#{term}%")
  else
    all
  end
end

application.js

$(function() {
  $("product_search input").keyup(function() {
    $.get($("#product_search").attr("action"), $("#product_search").serialize(), null, "script");
  return false;
  });
});

index.js.erb

$("#products").html("<%= escape_javascript render("products") %>");

UPDATE:

application.js

$(document).on('turbolinks:load', (function() {
  $("product_search input").keyup(function() {
    $.get($("#product_search").attr("action"),  $("#product_search").serialize(), null, "script");
  return false;
  });
}));
0 Answers
Related