# Controller
class UserController < ApplicationController
autocomplete_for :user, :name
end
#Customized....
autocomplete_for :user, :name do |items,method|
render :text => items.map{|item| "#{item.send(method)} -- #{item.id}"}.join("\n")
end
# View
<%= f.text_field :auto_user_name, :class => 'autocomplete', 'autocomplete_url'=>autocomplete_for_user_name_users_path %>
# Routes
map.resources :users, :collection => { :autocomplete_for_user_name => :get}
#JS
#any library you like
#(includes examples for jquery jquery.js + jquery.autocomplete.js + jquery.autocomplete.css )
jQuery(function($){//on document ready
//autocomplete
$('input.autocomplete').each(function(){
var input = $(this);
input.autocomplete(input.attr('autocomplete_url'));
});
});
#Model(input/output association)
class User
find_by_autocomplete('name')
end
class Post
autocomplete_for('user','name') # auto_user_name= + auto_user_name
end
.
Not as thought free as the default version, but gives you a lot more control.
script/plugin install git://github.com/grosser/simple_auto_complete.git
README


8 comments
Comments feed for this article
April 30, 2008 at 9:00
Pages tagged "unobtrusive"
[...] tagged unobtrusiveOwn a WordPress blog? Make monetization easier with the WP Affiliate Pro plugin. Unobtrusive Autocomplete Rails Plugin saved by 5 others stephaniehho bookmarked on 04/30/08 | [...]
June 19, 2008 at 2:21
Overcame
Somehow i missed the point. Probably lost in translation
Anyway … nice blog to visit.
cheers, Overcame.
July 29, 2009 at 6:42
R
Is there a way to override the query to use a custom query?
July 29, 2009 at 7:10
pragmatig
you can pass e.g. :conditions/:limit/:order etc, and if thats not enought you could add a simple hack(please send me too) to use an option like :scope=>Product.valid.sane
July 29, 2009 at 22:42
R
Thanks for the quick reply, very useful plugin and thanks for developing it.
I can pass conditions, but is there a way to pass the param (q) to the conditions? It might be useful to be able to override this in the model (I would try to modify it but have no experience writing plugins).
Thanks
July 30, 2009 at 6:41
pragmatig
you mean like this ?
:conditions=>["name like ?", params[:q].to_s+’%']
July 30, 2009 at 20:09
R
Yes, but I get the error:
undefined local variable or method ‘params’
in the controller when I try to do that. Is this in the right spot?
Thanks
July 31, 2009 at 5:54
pragmatig
hmm that does not work since its called in the class and not when the method is actually called. Maybe just copy-paste the actual method definition and insert the stuff you need.