Module: AutocompleteViewHelpers
- Defined in:
- lib/autocomplete_view_helpers.rb
Overview
Sunspot Autocomplete
Sunspot Autocomplete is a Rails plugin that lets you use Solr and Sunspot for handy autocompletion of your html text inputs.
Features:
- Autocomplete: Typing "clo" will yield results that start with "clo", like "cloudy with a chance of meatballs".
- Autosuggest: Typing "clo" will yield results that contain (or start with) "clo", like "cloudy with a chance of meatballs" and "Jumping like Clowns".
- Both features are case insenitive.
- A CSS based view. You can override some style rules to force your look and feel.
Prerequisites
You should have solr, sunspot and sunspot_rails ON and running.
http://outoftime.github.com/sunspot
Installation
Download the plugin and place it under vendor/plugins.
Run the following rake task to copy the plugin's assets to your public directory. This will copy jquery.js and solr-autocompleter to your public/javascripts.
rake sunspot_autocomplete:copy_assets
Usage
In your solr schema.xml, in addition to field types added by sunspot, add the following field types inside the
Also in your solr schema.xml, in addition to fields added by sunspot, add the following fields inside thw
To be able to autocomplete/autosuggest a model's attribute, call 'autocomplete'/'autosuggest' on it in its 'searchable' block. the field_name used (post_title and post_author in the following example) must be unique across all your autocomplete fields of the application.
class Post < ActiveRecord::Base searchable do autocomplete :post_title, :using => :title autosuggest :post_author, :using => :author end end
In your view, Add the following script tags (in the given order) to be able to use the view helpers.
Also, add the following stylesheet to use the basic style included. Alternatively, you can override those style rules to force your design's look and feel.
In your view, to create a text field with autocomplete:
<%=autocomplete_text_field "post", "title", "http://127.0.0.1:8983/solr/", "post_title"%>
And to create a text field with autosuggest:
<%=autosuggest_text_field "post", "author", "http://127.0.0.1:8983/solr/", "post_author"%>
You can view documentation for more advanced features of the helpers.
Instance Method Summary collapse
-
#autocomplete_text_field(object_name, method, solr_url, autocomplete_field_name, html_options = {}, autocomplete_options = {}) ⇒ Object
Generates a text input using the given
object_nameandmethod. -
#autosuggest_text_field(object_name, method, solr_url, autocomplete_field_name, html_options = {}, autocomplete_options = {}) ⇒ Object
Generates a text input using the given
object_nameandmethod.
Instance Method Details
#autocomplete_text_field(object_name, method, solr_url, autocomplete_field_name, html_options = {}, autocomplete_options = {}) ⇒ Object
Generates a text input using the given object_name and method.
The generated text field autocompletes given solr_url: the url to your solr instance (e.g. http://127.0.0.1:8983/solr/)
Autocompletion is fetching results that only begins with the given part of word.
autocomplete_field_name is the unique field_name assigned in your model's searchable block
html_options are regular HTML options like :class and :id
autocomplete_options are advanced options for autocompletion http://docs.jquery.com/Plugins/Autocomplete/autocomplete#toptions
100 101 102 |
# File 'lib/autocomplete_view_helpers.rb', line 100 def autocomplete_text_field(object_name, method, solr_url, autocomplete_field_name, ={}, ={}) autocomplete_stub object_name, method, solr_url, autocomplete_field_name, false, , end |
#autosuggest_text_field(object_name, method, solr_url, autocomplete_field_name, html_options = {}, autocomplete_options = {}) ⇒ Object
Generates a text input using the given object_name and method.
The generated text field autosuggests given solr_url: the url to your solr instance (e.g. http://127.0.0.1:8983/solr/)
Autosuggestion is fetching results that begins with/ends with/contains the given part of word.
autocomplete_field_name is the unique field_name assigned in your model's searchable block
html_options are regular HTML options like :class and :id
autocomplete_options are advanced options for autocompletion http://docs.jquery.com/Plugins/Autocomplete/autocomplete#toptions
110 111 112 |
# File 'lib/autocomplete_view_helpers.rb', line 110 def autosuggest_text_field(object_name, method, solr_url, autocomplete_field_name, ={}, ={}) autocomplete_stub object_name, method, solr_url, autocomplete_field_name, true, , end |