0

I'm trying to implement a JQuery-ui autocomplete on a search form_tag. I have followed this in order to implement it.

This is what I have done thus far but without the desired results:

  • Installed the Jquery-ui manually and thus 0 errors show in the browser source inspector.
  • I have added the jQuery code for autocompletion.
  • I have generated the search_suggestion resource.

And added the following to the search_suggestion_controller.rb in order to test if the json is rendered:

class SearchSuggestionsController < ApplicationController
  def index
    render json: %w[foo bar]
  end
end

But absolutely nothing renders/shows as it is supposed to do!

Does anyone have any idea why this is happening?

Here is my other code:

<%= form_tag items_path, method: :get do %>
  <%= text_field_tag :search, params[:search], autofocus: true, placeholder: 'Enter keyword to search', class: "search-query search_size" %>
  <%= submit_tag 'Search', style: "display: none;" %>
<% end %>

items_controller.rb

def search
  if params[:search]
    @items = Item.search(params[:search]).order("created_at DESC")
    respond_to do |format|
      format.html
      format.json { render json: @items.map(&:title) }
    end
  else
    @items = []
  end
end

item.rb

def self.search(term)
  return where("0=1") if term !~ /\w{4}/
  where("lower(title) LIKE lower(:term)", term: "%#{term}%")
end

items.coffee

jQuery ->
$('#search').autocomplete
 source: "/search_suggestions"
3
  • Do you get valid JSON when you navigate to /search_suggestions in your web browser? Commented Nov 15, 2017 at 15:24
  • Thanks for the reply @Daniel Westendorf... Yes I do get["foo","bar"], but its not showing on the searchbox as its suppose to.
    – Theopap
    Commented Nov 15, 2017 at 15:25
  • Hey - couple of questions here: + Does the element definitely have the id #search when it's rendered on the page? + Have you put in any debug points to confirm that a) the autocomplete hits the controller and b) it gets the json back in the response (you can use the response listener for this)?
    – SRack
    Commented Nov 16, 2017 at 10:03

0