forked from patshaughnessy/auto_complete
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME
91 lines (65 loc) · 2.82 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
Fork of the standard auto_complete plugin to support:
1. Creating auto complete text fields that can be repeated more than once a single form.
2. Using auto_complete_for with named scopes to display a customized list
of autocomplete options.
See: http://patshaughnessy.net/repeated_auto_complete for details.
Repeated autocomplete text fields:
==================================
A "text_field_with_auto_complete" method is made available to the form builder
yielded by form_for and fields_for that:
- Insures unique id's for <input> and <div> tags by inserting unique integers into their
id attributes
- Uses the object name from the surrounding call to form_for or fields_for so attribute
mass assignment will work as usual
- Works with the same server side controller method, auto_complete_for, as usual
- Supports nested attributes in Rails 2.3
form.text_field_with_auto_complete works the same as the original text_field_with_auto_complete macro,
except it does not take the object as a parameter.
Example with nested attributes using Rails 2.3 or later:
class Project < ActiveRecord::Base
has_many :tasks
accepts_nested_attributes_for :tasks, :allow_destroy => true
end
<% form_for @project do |project_form| %>
<p>
<%= project_form.label :name, "Project:" %>
<%= project_form.text_field_with_auto_complete :name, {}, {:method => :get } %>
</p>
<% project_form.fields_for :tasks do |task_form| %>
<p>
<%= task_form.label :name, "Task:" %>
<%= task_form.text_field_with_auto_complete :name, {}, { :method => :get, :skip_style => true } %>
</p>
<% end %>
<% end %>
Rails 2.2 and earlier example:
<% for person in @group.people %>
<% fields_for "group[person_attributes][]", person do |person_form| %>
<p>
Person <%= person_form.label :name %><br/>
<%= person_form.text_field_with_auto_complete :name, {}, {:method => :get } %>
</p>
<% end %>
<% end %>
Named scopes with auto_complete_for:
====================================
auto_complete_for now optionally accepts a block that is called with the item list and HTTP parameters
when the auto complete AJAX request is received. This block can be used to specify that a named scope
be used to generate a customized list of autocomplete options.
Example using anonymous scope:
auto_complete_for :some_model, :some_other_field do |items, params|
items.scoped( { :conditions => [ "a_third_field = ?", params['some_model']['a_third_field'] ] })
end
Example using named scope:
class Task < ActiveRecord::Base
belongs_to :project
named_scope :by_project,
lambda { |project_name| {
:include => :project,
:conditions => [ "projects.name = ?", project_name ]
} }
end
auto_complete_for :task, :name do | items, params |
items.by_project(params['project'])
end
Copyright (c) 2009 [Pat Shaughnessy], released under the MIT license