This view helper generates html output for a Twitter Bootstrap styled control-group
(input fields and check_boxes).
Basic usage in a view html.erb file (pass in the Model and the Attribute name):
<%= control_container(@user, :name) %>
It renders the following:
- a label
- textbox (or other field type specified in options hash)
- a help text string (if passed)
Example HTML output:
<div class="control-group">
<label class="control-label" for="user_name">Name</label>
<div class="controls">
<input id="user_name" name="user[name]" size="30" type="text" value="John Smith" />
</div>
</div>
Label text will default to the model attribute name. You can override it in the options hash. Help text (if passed) will be located below the textbox by default, but you can pass a hash option to have the help text placed inline with the control.
You can pass these options in an options hash:
- :label_text
- :help_text
- :help_inline
- :field_type
(default field_type is "text_field",
other options: "text_area", "email_field", "password_field", "url_field" "check_box", etc.)
Here's an example of how to use the options hash:
<%= control_container(@user, :password, {:label_text => "New password",
:help_text => "(leave blank if you don't want to change it)",
:help_inline => true,
:field_type => "password_field"}) %>
HTML output from above:
<div class="control-group">
<label class="control-label" for="user_password">New password</label>
<div class="controls">
<input id="user_password" name="user[password]" size="30" type="password" />
<span class="help-inline"><i>(leave blank if you don't want to change it)</i></span>
</div>
</div>
Learn more about creating helpers here: http://vanderburg.org/Speaking/CustomRailsHelpers.pdf