Hobo Cookbook

View Source



An <input type="text"> with auto-completion. Allows the user to chose the target of a belongs_to association by name.

This tag relies on an autocompleter being defined in a controller. A simple example:

<form with="&ProjectMembership.new">

class ProjectMembership < ActiveRecord::Base
  belongs_to :user

class User < ActiveRecord::Base
  has_many :project_memberships, :accessible => true, :dependent => :destroy

class UsersController < ApplicationController

The route used by the autocompleter looks something like /users/complete_name. The first part of this route is specified by the complete-target attribute, and the second part is specified by the completer attribute.

complete-target specifies the controller for the route. It can be specified by either supplying a model class or a model. If a model is supplied, the id of the model is passed as a parameter to the controller. (?id=7, for example) The default for this attribute is the class of the context. In other words, the class that contains the has_many / has_one, not the class with the belongs_to.

completer specifies the action for the route. name-one prepends complete_ to the value given here. This should be exactly the same as the first parameter to autocomplete in your controller. As an example: autocomplete :email_address would correspond to completer="email_address". The default for this attribute is the name field for the model being searched, which is usually name, but not always.

The query string is passed to the controller in the query parameter. (?query=hello for example).

For more information on how to customize the controller, see the controller manual

Here’s a more complex example. This used to be a part of agility until it was simplified.

class ProjectsController < ApplicationController
  autocomplete :new_member_name do
    project = find_instance
    hobo_completions :name, User.without_project(project).is_not(project.owner)

Note that this was added to the projects controller, rather than the users controller as in the first example. You can read this as: create an auto-complete action called new_member_name that finds users that are not already members of the project, and not the owner of the project, and completes the :name field.

<name-one:user complete-target="&@project" completer="new_member_name"/>

We’re using an object as the complete-target rather than a class. This allows the find_instance in our controller action to function.

There’s another example of <name-one> use in the recipes.


  • complete-target, completer: see above
  • min-chars: The minimum number of characters that must be entered in the input field before an Ajax request is made.
  • nil-value: If there is no current value, this text will appear greyed out inside the control, and will disappear on focus.


If you wish to set min-chars to 0, you will require this patch to controls.js. ‘controls.js’ was added to your project via the rails generator, not via Hobo.

Show Source


No Comments to display