Module: JqueryAutocomplete::ClassMethods
- Defined in:
- lib/jquery_autocomplete.rb
Overview
Example:
# Controller
class BlogController < ApplicationController
auto_complete_for :post, :title
end
# View
<%= text_field_with_auto_complete :post, title %>
By default, auto_complete_for limits the results to 10 entries, and sorts by the given field.
auto_complete_for takes a third parameter, an options hash to the find method used to search for the records:
auto_complete_for :post, :title, :limit => 15, :order => 'created_at DESC'
def auto_complete_for(object_name, method_name, = {})
Instance Method Summary collapse
Instance Method Details
#auto_complete_for(object_name, method_name, options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jquery_autocomplete.rb', line 27 def auto_complete_for(object_name, method_name, = {}) self.send(:define_method, "auto_complete_for_#{object_name}_#{method_name}") do model = object_name.to_s.camelize.constantize full_method_name = "#{model.quoted_table_name}.#{method_name}" = { :conditions => ["LOWER(#{full_method_name}) LIKE ?", '%' + params['term'].downcase + '%'], :order => "#{full_method_name} ASC", :limit => 10 }.merge!() @items = model.scoped() @items = yield(@items, params) if block_given? render :inline => "<%= auto_complete_result @items, '#{method_name}', '#{params['term']}' %>" end end |