Alex Moore
2006-Aug-01 12:29 UTC
[Rails] Custom Autocomplete Field - Making a field lowercase
I''m trying to humanize() the results that appear in the autocomplet drop down list. First i customised the controller. In the render line on the controller it is saying "i want to use locality on each of the @items". So locality is what i want to humanize". My ideas so far is to: Customise the PostCode object so that when auto_complete_result trys to call item.locality, but i''ve tried overridding both def locality and def[] and it doesn''t seam to work. The other idea is to create a rhtml template that generates the auto_complete_return.. but I have no idea how to do that. Here''s the code: View: <%= text_field_with_auto_complete :post_code, :locality, :cols => 40 %> Controller: def auto_complete_for_post_code_locality find_options = { :conditions => [ "LOWER(locality) LIKE ?", params[:post_code][:locality].downcase + ''%'' ], :order => "locality ASC", :limit => 10 } @items = PostCode.find(:all, find_options) render :inline => "<%= auto_complete_result @items, ''locality'' %>" end def auto_complete_result(entries, field, phrase = nil) return unless entries items = entries.map { |entry| content_tag("li", phrase ? highlight(entry[field], phrase) : h(entry[field])) } content_tag("ul", items.uniq) end