-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrepeated_auto_complete.html
46 lines (46 loc) · 7.5 KB
/
repeated_auto_complete.html
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
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>The auto_complete plugin refactored to support repeated fields and named scopes - Pat Shaughnessy</title><meta name="description" content=""><meta name="author" content=""><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="/assets/css/normalize.css"><link rel="stylesheet" href="/assets/css/skeleton.css"><link rel="alternate" type="application/atom+xml" title="Pat Shaughnessy - Feed" href="http://feeds2.feedburner.com/patshaughnessy"><link rel="icon" type="image/png" href="images/favicon.png"></head><body><div id="banner"><a href="/"><span id="title">Pat Shaughnessy</span><span id="tagline"> blogger, rubyist, aspiring author</span></a></div><div style="margin-top: 35px"><div class="ten columns"><div class="container"><div class="row"><article class="post"><header><h1>The auto_complete plugin refactored to support repeated fields and named scopes</h1><div class="metadata">November 18th 2008 — <a href="#disqus_thread" data-disqus-identifier="https://patshaughnessy.net/repeated_auto_complete"> Comments and Reactions</a></div></header><section class="content"><p>(Updated June 2009)</p>
<p>This version of auto_complete will support text fields that are repeated more than once on a complex form. It allows you to call text_field_with_auto_complete on the form builder object yielded by fields_for or form_for. This will work for complex forms built with Rails 2.2 or earlier, and for the nested attributes feature introduced in Rails 2.3. Here's an example using nested attributes:</p>
<pre><% form_for @project do |project_form| %>
<% 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 %></pre>
<p>It also allows you to provide a block to auto_complete_for in your controller that filters the drop down pick list in some custom way. For example, this block would display task names for the project the user had selected elsewhere on the same form, using a named scope by_project:</p>
<pre>auto_complete_for :task, :name do | items, params |
items.by_project(params['project'])
end</pre><br/>
<p><b>Code:</b> <a href="http://github.com/patshaughnessy/auto_complete">http://github.com/patshaughnessy/auto_complete</a></p>
<p><b>Install as a gem:</b></p>
<pre>gem sources -a http://gemcutter.org
sudo gem install repeated_auto_complete</pre><br/>
<p>… and in config/environment.rb:
<pre>Rails::Initializer.run do |config|
…
config.gem "repeated_auto_complete"
…
end</pre><br/>
<p><b>Install as a plugin:</b></p>
<pre>script/plugin install git://github.com/patshaughnessy/auto_complete.git</pre><br/>
<p><b>More information:</b>
<ul>
<li><a href="https://patshaughnessy.net/2009/11/25/scaffolding-for-auto-complete-on-a-complex-nested-form">How to generate scaffolding for auto_complete on a complex form</a></li>
<li><a href="https://patshaughnessy.net/2009/6/15/auto-complete-for-complex-forms-using-nested-attributes-in-rails-2-3">How the plugin works with Rails 2.3 nested attributes</a></li>
<li><a href="https://patshaughnessy.net/2009/6/15/repeated-auto-complete-plugin-usage-change">Recent usage changes (June 2009) to enable nested attribute support</a></li>
<li><a href="https://patshaughnessy.net/2009/1/30/repeated_auto_complete-changes-merged-into-auto_complete">Detailed description of code changes</a></li>
<li><a href="https://patshaughnessy.net/2009/1/30/sample-app-for-auto-complete-on-a-complex-form">Sample app showing how to use the plugin</a></li>
<li><a href="https://patshaughnessy.net/2009/4/3/filtering-auto_complete-pick-lists-part-2-using-named-scopes">Explanation of how to use named scopes with auto_complete_for</a></li>
<li><a href="https://patshaughnessy.net/2009/3/14/filtering-auto_complete-pick-lists">Related article about how to filter auto_complete pick lists</a></li>
<li><a href="https://patshaughnessy.net/2008/10/21/autocomplete-plugin-doesn-t-work-for-repeated-fields">Why the auto_complete plugin doesn’t work for repeated fields</a></li>
<li><a href="https://patshaughnessy.net/2008/11/16/testing-is-a-lesson-in-humility">My experience writing unit tests for the modified plugin</a></li>
<li><a href="https://patshaughnessy.net/2008/10/31/modifying-the-autocomplete-plugin-to-allow-repeated-fields">My original changes to auto_complete from October</a> (no longer used)</li>
</ul></p>
</section><section class="comments"><div id="disqus_thread"><script type="text/javascript">var disqus_identifier = 'https://patshaughnessy.net/repeated_auto_complete'; var disqus_shortname = 'patshaughnessy'; var disqus_title = 'The auto_complete plugin refactored to support repeated fields and named scopes';</script></div><script type="text/javascript" src="https://disqus.com/forums/patshaughnessy/embed.js"></script><noscript><a href="https://patshaughnessy.disqus.com/?url=ref">View the discussion thread.</a></noscript></section></article></div></div></div><div class="two columns"><div id="sidebar"><img src="/assets/images/pat.jpg"></img><div class="header">Subscribe</div><div class="links"><ul><li><a id="feed" href="http://feeds.feedburner.com/patshaughnessy"><img src="/assets/images/feed-icon16x16B.png"></img></a><a href="http://twitter.com/pat_shaughnessy"><img width="20" height="20" src="/assets/images/twitter.svg"></img></a></li></ul></div><div class="header">Buy My Book</div><div class="links"><ul><li><a href="/ruby-under-a-microscope"><img src="/assets/images/book-cover.png"></img></a></li><li id="eBook"><a href="/ruby-under-a-microscope">Ruby Under a Microscope</a></li></ul></div><div class="header">More on the auto_complete plugin</div><div class="links"><ul><li><a href="/2009/6/15/auto-complete-for-complex-forms-using-nested-attributes-in-rails-2-3">Auto complete for complex forms using nested attributes in Rails 2.3</a></li><li><a href="/2009/6/15/repeated-auto-complete-plugin-usage-change">Repeated auto complete plugin usage change</a></li><li><a href="/2009/4/3/filtering-auto_complete-pick-lists--part-2-using-named-scopes">Filtering auto_complete pick lists – part 2: using named scopes</a></li><li><a href="/2009/3/14/filtering-auto_complete-pick-lists">Filtering auto_complete pick lists</a></li></ul></div><div class="header">Popular</div><div class="links"><ul><li><a href="/2016/11/26/learning-to-read-x86-assembly-language">Learning to Read x86 Assembly Language</a></li><li><a href="/2012/1/4/never-create-ruby-strings-longer-than-23-characters">Never create Ruby strings longer than 23 characters</a></li><li><a href="/2012/3/23/why-you-should-be-excited-about-garbage-collection-in-ruby-2-0">Why You Should Be Excited About Garbage Collection in Ruby 2.0</a></li><li><a href="/2013/4/3/ruby-2-0-works-hard-so-you-can-be-lazy">Ruby 2.0 Works Hard So You Can Be Lazy</a></li></ul></div><div class="header"><a href="/">More...</a></div></div></div><script type="text/javascript">(function () {
var s = document.createElement('script'); s.async = true;
s.type = 'text/javascript';
s.src = 'https://' + disqus_shortname + '.disqus.com/count.js';
(document.getElementsByTagName('HEAD')[0] || document.getElementsByTagName('BODY')[0]).appendChild(s);
}());</script></div><div id="footer" class="ten columns"><p>Content and UI design © 2008-2022 Pat Shaughnessy</p></div></body></html>