This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README
70 lines (47 loc) · 1.74 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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 <[email protected]>, released under the MIT license