Search code examples
htmlruby-on-railstwitter-bootstrapruby-on-rails-2form-for

How to use Rails, form_for, and Bootstrap to generate properly nested <form>


I've inherited some old Rails 2.3 code. I want to use the standard rails <%= form_for %> and <%= remote_form_for %> helpers. I also want to use Twitter Bootstrap, and have the <label> <input> be properly nested in the Bootstrap classed divs, like so:

<div class="control-group">
  <label class="control-label" for="inputEmail">Email</label>
  <div class="controls">
    <input type="text" id="inputEmail" placeholder="Email">
  </div>
</div>

Is there a good gem/method to do this? We have TONS of <form> elements in our app, so ideally I'd love to have a helper method or gem to handle this for me.

I've previously used twitter-bootstrap-rails gem, but its asset-pipeline driven. We've considered adding the asset pipeline to Rails 2 using this method: http://pivotallabs.com/giving-rails-2-the-asset-pipeline/ but would like to consider solutions that don't rely on this as well.


Solution

  • I went ahead and forked a gem and backported it for Rails 2.3. It allows perfect Bootstrap 2.3 markup:

    https://github.com/Lordnibbler/twitter_bootstrap_form_for/blob/rails-2.3/