Unobtrusive Autocomplete Rails Plugin

Auto complete solution, with customizable results (return just the word or word+id…) and no framework dependency, just use a plain text_field + class=> autocomplete and every autocomplete libary you like (included: jQuery).

autocomplete

# 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

16 thoughts on “Unobtrusive Autocomplete Rails Plugin

  1. 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

  2. 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

  3. 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

  4. 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.

  5. you can give finder options in the options for the controller method, or just define the method yourself, just copy/past whats inside the define_method block

  6. Do you know why I would be getting this java error:
    Uncaught TypeError: undefined is not a function ?

    I followed the instructions step by step and can’t seem to get it working. I’m working with rails 2.3.5

    Thanks for your time.

  7. Yeah I don’t understand. Everything seems to work fine but right blow the views code I get this error. I see it in my console when using a developer tool like firebug.

    Thanks for letting me know at least its not a version issue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s