Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do i fix my jquery-ui autocomplete problem (details below)?

Kosta Pontidas asked in General

I followed your tutorial on elastic search/search kick and implementing jquery autocomplete. It works when I use "Test" as the data, however when I try to use my data from posts table, it doesn't work.

So when my posts controller file's autocomplete action looks like this, it works:

  def autocomplete
       render json: ["Test"]
  end

When I change it to this, and add the changes to the post model file, it stops working -

controller file:

    def autocomplete
         render json: Post.search(params[:term], fields: [{title: :text_start}], limit: 10).map(&:title)
     end

post.rb model file:

     searchkick text_start: [:title]

any help is much appreciated. It's really starting to bug me because it should be quick and easy like your tutorial, I have asked multiple questions on stack overflow, no response and researched for days, spent hours doing several different tutorials with different methods. I subscribed to your website hoping you could help. thank you

Reply

Hey Kosta,

I'm not sure entirely what's wrong, but here's what I would do:

  1. Take this and run it in your rails console: Post.search(params[:term], fields: [{title: :text_start}], limit: 10).map(&:title)
  2. If this doesn't return an array of string like the test example, then you know this is the problem and you can start fiddling with that to get it to return the right format. Maybe you just needed to reindex or something simple.
  3. If it does return an array of strings, then go into your browser and check that the JSON it receives is correct. You can print it out in the browser with a console.log to verify that.
  4. If you are receiving the correct JSON in the browser, then you can check to make sure this gets passed over to the autocomplete correctly. You know this probably isn't the case because the test JSON did work, so it's the least likely to be the problem.

That should help you break apart the problem to figure out what's the piece causing the problem and it'll probably be obvious once you get there. :)

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.