Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A helper class to look up items and update a hidden id number

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README
Octocat-spinner-32 Rakefile
Octocat-spinner-32 init.rb
Octocat-spinner-32 install.rb
Octocat-spinner-32 uninstall.rb
README
JqueryAutocompleteHelper
==================

This helper uses jquery's autocomplete to look up a value and insert it into a hidden form field.  
Useful for linking a join model to another model when there are too many records for a select field.


Example
=======

This plugin assumes:

The model being searched has a method called display_name:

class Person < ActiveRecord::Base
 def display_name
   first_name + " " + last_name
 end
end

view:

<%= form_for @organization do |f| %>
  <p>
    <%= f.label :person_id %><br />
   <%= f.autocomplete_field(:person_id. :url => people_path) %>
  </p>
....
<%= yield :runview %>


The controller for person needs to search based on params[:q] :

def index
  @people = Person.find(:all, :conditions => ["first_name LIKE ? or last_name LIKE ?", '%'+q + '%', '%'+q + '%' ], :limit => 20)

  respond_to do |format|
    format.html  { ... }
    format.xml  { ... }
    format.js { render :text => @people.map { |c| "#{c.display_name}|#{c.id}\n" } }
  end
end

Formtastic forms are also supported:

<%= semantic_form_for @organization do |f| %>
<% f.inputs do %>
   <%= f.input :person_id, :as => :autocomplete, :url => people_path %>
<% end %>
....
<%= yield :runview %>

Override the display value with :display_value if the relation name doesn't match the id name:
<%= f.input :parent_id, :as => :autocomplete, :url => people_path, :display_value => @person.household_parent.display_name %>

Add a javascript callback to add new models vi :new_callback

<script type="text/javascript">
  function open_person_form() {
  ...   
}
</script>


 <%= f.input :person_id, :as => :autocomplete, :url => people_path, :new_callback => "open_person_form();" %>




Copyright (c) 2009 David Morton <dgm@dgrmm.net>, released under the MIT license
Something went wrong with that request. Please try again.