...">

Implementing Autocomplete Search

I am not sure how to create an autocomplete form for my search function.

<%= form_tag "/search", :method => "get" do %>
    <%= text_field_tag :query, params[:query] %>
    <%= image_submit_tag "site/blankimg.png", :name => nil %>
<% end %>

I have a next controller that has a configured action

   def query
     @users= Search.user(params[:query])
     @article= Search.article(params[:query])
   end

The model has the following form:

def self.user(search)
 if search
    User.find(:all, :conditions => ['first_name LIKE ?', "%#{search}%"])
  else
    User.find(:all)
  end
end

def self.article(search)
  if search
    Article.find(:all, :conditions => ['title LIKE ?', "%#{search}%"])
  else
    Article.find(:all)
  end
end

Now this work is great for searching, but I want it to show me the result, I am writing this, and I cannot use jquery autocomplete, because these are two objects.

+5
source share
1 answer

Use Twitter . Here are some examples:

http://twitter.github.com/typeahead.js/examples/

Typeahead twitter and all the necessary information is available from https://github.com/twitter/typeahead.js/

How to use

, "". , ( ), :

$('input.typeahead').typeahead({
  local: ['suggestion1', 'suggestion2', 'suggestion3',...., 'suggestionN']
  #The typeahead is going to load suggestions from data in local.
});

, , :

Controller:
def load_suggestions
  @suggestions = MyModel.select(:name) or MyModel.find(:all, conditions: [...]) #Select the data you want to load on the typeahead.
  render json: @suggestions
end

JS file:
$('input.typeahead').typeahead([
  {
    prefetch: 'https://www.mipage.com/suggestion.json' #route to the method 'load_suggestions'
    #This way, typeahead will prefecth the data from the remote url
   }
]);
+21

All Articles