public
Description: An enhanced select helper for forms that allows you to use the full HTML spec for select option elements.
Homepage: http://www.aeonscope.net
Clone URL: git://github.com/aeonscope/enhanced_select.git
README.rdoc

Overview

An enhanced select helper for forms that allows you to use the full HTML spec for select option elements. This is more than what you get with the default Rails select helper which only allows you to specify the value, text, and selected/disabled attributes.

License

Copyright © 2009 Brooke Kuhlmann of Berserk Technologies. See the included LICENSE for more info.

History

See the CHANGELOG file for more info.

Requirements

  1. Ruby on Rails.

Installation

Type the following from the command line to install:

  • UNIX: sudo gem install aeonscope-enhanced_select
  • Windows: gem install aeonscope-enhanced_select

Update your environment.rb file to include the new gem:

  • config.gem "aeonscope-enhanced_select", :lib => "enhanced_select", :source => "gems.github.com"

Usage

Simply use "enhanced_select" instead of the Rails "select" helper method in your views:

  <% form_for @city do |form| %>
    <div><%= form.enhanced_select :id, @schools, :include_blank => "-select-" %></div>
  <% end %>

This will yield the following results:

  <select id="<auto filled>" name="<auto filled>">
    <option value="">-select-</option>
    <option value="1">Goldbugs</option>
    <option value="2">Falcons</option>
  </select>

The enhanced_select method takes all the standard options as the default Rails select method. The only difference is that for the options, it expects an array of hashes. So using the @schools variable above, we would have filled it with the following data:

  @schools = [{:value => 1, :text => "Goldbugs"}, {:value => 2, :text => "Falcons"}]

NOTE: While enhanced_select accepts a hash where all keys will be represented as attribute keys in the resulting HTML, the :text key is reserved for specifying the content you wish to display between the resulting <option></option> tags.

While the above is a simple example, lets look at a more interesting case. Lets say we want each school to have a CSS class associated with it so that we might be able to attach jQuery behaviors to each option (that and/or we want to style each option too). Here is how things might play out:

Model Code

  class City < ActiveRecord::Base
    has_many :schools
  end

  class School < ActiveRecord::Base
    belongs_to :city

    def kind
      [(public? ? "public" : nil), (private? ? "private" : nil)].compact * ' '
    end
  end

Controller Code

  class CityController < ApplicationController
    include Rest
    before_filter :form_data, :only => [:new, :create, :edit, :update]

    private

    def form_data
      @schools = School.all.map {|school| {:value => school.id, :class => school.kind, :text => school.label}}
    end
  end

View Code

  <% form_for @city do |form| %>
    <div><%= form.enhanced_select :id, @schools, :include_blank => "-select-" %></div>
  <% end %>

This would yield the following results:

  <select id="<auto filled>" name="<auto filled>">
    <option value="">-select-</option>
    <option value="1" class="private">Goldbugs</option>
    <option value="2" class="public">Falcons</option>
  </select>

The possibilities are up to you as you can definitely build a hash array of options with more attributes than value, class, and text as shown above. The best part is that you have total control over how select options are built within your views.

Contact/Feedback/Issues