Rails Auto Completion

I am glad to see how simple it is to add an auto-completion text field to a Rails app. Since I want this available to all my controllers, I stuck the controller code in ApplicationController:

1
2
3
class ApplicationController < ActionController::Base
  auto_complete_for :recipe, :name
end

Then to create the simple form, I just add the following code into my layout file:

1
2
3
4
<%= form_tag :controller => "recipe", 
    :action => "find_recipe_for_name" %>
  <%= text_field_with_auto_complete :recipe, :name, {:size => '12'} %>
<%= end_form_tag %>

Finally, I set up my action:

1
2
3
4
5
def find_recipe_for_name
  @recipe = Recipe.find(:first, 
    :conditions => ["name = ?", params[:recipe][:name]])
  render :template => 'recipe/show'
end
Advertisements

Pages

Categories

Copyright ©2008-2013 James E Orchard-Hays, All Rights Reserved

%d bloggers like this: