Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter-Bootstrap and Forms in Rails

This may be a beginners question but not having much luck getting this going. The background:

  1. Rails 3.0.x version
  2. Bootstrap 2.0 - using as pre-compiled simply placed into public/stylesheets. Att
  3. Attempting to do a form with horizontal layout, i.e. label next to the input field on the same line.

The issue is that I can't get the form to work, but can get the default Bootstrap sample code, in the same form to layout correctly. The Rails form looks like:

<div class="row-fluid">
  <div class="span4 well">
    <%= form_for @member, :html => {:class => "form-horizontal"} do |m| %>
        <fieldset>

          <%= m.label :title  %>
          <%= m.text_field :title %>   

          <%= m.label :first_name %>
          <%= m.text_field :first_name %>
    <% end %>
   </div>
</div>

Notice that the form_for method has the class of form-horizontal as described by in Bootsrap form CSS

When displayed the label, for example title, is on one line left adjusted and then on the next line is the input field, also left adjusted.

Now if I include some of the sample Bootstrap code for forms such as:

<div>
  <form class="form-horizontal">
    <fieldset>
      <legend>Legend text</legend>
      <div class="control-group">
        <label class="control-label" for="input01">Text input</label>

        <div class="controls">
          <input type="text" class="input-xlarge" id="input01">

          <p class="help-block">Supporting help text</p>
        </div>
      </div>
    </fieldset>
  </form>
</div>

Then I get the expected outcome -- label and input field on the same line in horizontal layout.

I've tried adding the style "control-label" onto the m.label and m.text_field components but no joy in getting the layout as horizontal.

Thus, not sure what the issue is, since the working part validates that I can get Bootstrap to work, although with my Rails Form it's not honouring the form-horizontal.

like image 704
Grant Sayer Avatar asked Feb 22 '12 07:02

Grant Sayer


2 Answers

If you want to do the form vanilla (as in, without simpleform-bootstrap or other gems), you need to shape the form HTML accordingly:

<div class="row-fluid">
  <div class="span4 well">
    <%= form_for @member, :html => {:class => "form-horizontal"} do |m| %>
        <fieldset>
          <legend>Member Form</legend>
          <div class="control-group">
            <%= m.label :title, :class => "control-label" %>
            <div class="controls">
              <%= m.text_field :title, :class => "input-xlarge" %>
            </div>
          </div>

          <div class="form-actions">
            <%= m.submit :class => "btn btn-primary" %>
          </div>
        </fieldset>
    <% end %>
  </div>
</div>

As Arun Kumar Arjunan says, the form builder is not generating the html needed for the bootstrap framework.

Definitely check out the HTML needed in the bootstrap example pages, and/or by inspecting the DOM (i found that it's not really well documented). All in all, you can do it manually, like above, by creating your own form builder, or by using various gems.

Also as a sidenote, make sure to check out how to customize the errors css, because by default, when a validation error occurs, the field is wrapped by a <div class ="field_with_errors"> breaking the css selectors used by bootstrap.

like image 134
shuriu Avatar answered Sep 30 '22 03:09

shuriu


The problem is the field helpers are not generating the proper html for twitter bootstrap.

You can use simple_form gem which supports twitter bootstrap. See the documentation:

https://github.com/plataformatec/simple_form

Here is the example application: https://github.com/rafaelfranca/simple_form-bootstrap

Live Demo: http://simple-form-bootstrap.plataformatec.com.br/

like image 22
Arun Kumar Arjunan Avatar answered Sep 30 '22 04:09

Arun Kumar Arjunan