Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (45 sloc) 2.29 KB

Twitter Bootstrap Forms

twitter_bootstrap_forms is a Rails FormBuilder DSL that, like Formtastic, makes it easier to create semantically awesome, readily-stylable, and wonderfully accessible HTML forms in your Rails applications. It abides by the markup expectations of Twitter Bootstrap, make your forms look great right out of the box.

However, it also tries not to do too much. Rather than try to guess at input types and provide an exhaustive set of options for each tag helper (as Formtastic does), it only lightly wraps the existing Rails form tag helpers.


Besides Rails, Twitter Bootstrap depends on Haml. You don't have to use it yourself, but it made the views for this gem far, far cleaner and more readable.

I may consider adding a dependency on less-rails-bootstrap in the future, to tie this plugin to specific releases of Twitter Bootstrap.


= twitter_bootstrap_form_for @user do |user|

/ wraps a section in a fieldset with the provided legend text
= user.inputs 'Sign up' do

  / generates an email field with descriptive help text and standard
  / HTML attributes
  = user.email_field :email, :placeholder => '' do No account? #{link_to('Sign up!', '#')}

  / generates a password field in a similar manner
  = user.password_field :password do link_to('Forgot your password?', '#')

  / a field with a custom label
  = user.password_field :password_confirmation, 'Confirm Password'

  / input fields with custom add-ons
  = user.text_field :twitter_id, 'Twitter', :class => 'medium', :add_on => :prepend do
    %span.add-on @

  / lists of checkboxes / radio buttons
  = user.toggles 'Agreements' do
    = user.check_box :agree, 'I agree to the Terms and Conditions'
    = user.check_box :spam,  'I agree to receive all sorts of spam'

= user.actions do
  = user.submit 'Sign up'
  = button_tag 'Cancel', :type => 'reset', :class => 'btn'

That's it. All of the Rails field helpers you know and love work just like their normal FormBuilder counterparts, but with minor extensions to expose the functionality anticipated by Twitter Bootstrap.

Something went wrong with that request. Please try again.