Autocomplete in rails with soulmate.js
I've included the soulmate.js in my project, the page of the script is
https://github.com/mcrowe/soulmate.js . Most of the code are similar with
the usage. The soulmate is installed, and the redis can index correctly.
And the controller is
def autocomplete
render :json => Ec.search(params[:term])
end
The module is
class Ec < ActiveRecord::Base
acts_as_commentable
belongs_to :dvp
belongs_to :domain
after_save :load_into_soulmate
before_destroy :remove_from_soulmate
searchable do
text :name, :restriction,:description, :message
string :dvp_id
end
def load_into_soulmate
loader = Soulmate::Loader.new('ec')
loader.add("term"=>name,"id"=>id)
end
def remove_from_soulmate
loader = Soulmate::Loader.new('ec')
loader.remove("id" => self.id)
end
def self.search(term)
ecs = Soulmate::Matcher.new("ec").matches_for_term(term)
ecs.collect{|e| {"id"=>e["id"],"name"=>e["term"]}}
end
end
So the autocomplete in the controller can call the search in the module.
And in the page I pasted the script, which is from the soulmate.js.
<script type="text/javascript">
(function() {
var render, select;
$('#search-input').focus();
render = function(term, data, type) {
return term;
};
select = function(term, data, type) {
return console.log("Selected " + term);
};
$('#search-input').soulmate({
url: location.origin+'/ecs/19/auto_complete_by_dvp',
types: ['name'],
renderCallback: render,
selectCallback: select,
minQueryLength: 2,
maxResults: 10
});
}).call(this);
</script>
Right now, I think the search function works well. Because if I output the
search result, the terminal will show it like [{"id"=>257, "name"=>"Estel
Towne MD"}, {"id"=>232, "name"=>"Miss Estel Schmeler"}] .
My question is I don't know how to display the result list in the page,
and I'm not sure whether I need other script to display it.
No comments:
Post a Comment