classy_forms

Created: 2008-11-24 05:09
Updated: 2016-05-08 17:41
License: mit

README.markdown

classy_forms

A form builder for Rails with labels, class attributes for input tags and disabled input tags.

WHY?

Look at all the cruft in the following...

<%- form_for @user do |f| -%>
  <%= f.error_messages %>
  <div class='input text'>
    <%= label :display_name %>
    <%= f.text_field :display_name %>
  </div>
  <div class='input text'>
    <%= label :email, 'Email <span class="required">* required</span>' %>
    <%= f.text_field :email %>
  </div>
  <div class='input text disabled'>
    <%= label :domain %>
    <%= f.text_field :domain, :disabled => true %>
  </div>
  <div class='input submit'>
    <%= f.submit "Update" %>
  </div>
<%- end -%>

Even when using HAML, there's all those extra .input div tags...

- form_for @user do |f|
  = f.error_messages
  .input.text
    = label :display_name
    = f.text_field :display_name
  .input.text
    = label :email, 'Email <span class="required">* required</span>'
    = f.text_field :email
  .input.text.disabled
    = label :domain
    = f.text_field :domain, :disabled => true
  .input.submit
    = f.submit "Update"

Installation

Install the plugin in your rails project from the command-line...

$ cd /path/to/your/project
$ script/plugin install git://github.com/sbfaulkner/classy_forms.git

Add 'config/initializers/classy_forms.rb' to your project with the following line...

ActionView::Base.default_form_builder = ClassyFormBuilder

Example

From the example shown above, you can instead...

<%- form_for @user do |f| -%>
  <%= f.error_messages %>
  <%= f.text_field :display_name %>
  <%= f.text_field :email, :required => true %>
  <%= f.text_field :domain, :disabled => true %>
  <%= f.submit "Update" %>
<%- end -%>

Using HAML it's a bit nicer yet (but most of the benefit has already been realized)...

- form_for @user do |f|
  = f.error_messages
  = form.text_field :display_name
  = form.text_field :email, :required => true
  = f.text_field :domain, :disabled => true
  = f.submit "Update"

TODO

  • label radio buttons and checkboxes with their value rather than their field name
  • group radio buttons in a single wrapper

Legal

Author: S. Brent Faulkner brentf@unwwwired.net
License: Copyright © 2008-2009 unwwwired.net, released under the MIT license

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more