Skip to content

Instantly share code, notes, and snippets.

@mattslay mattslay/Example.md
Created Apr 27, 2012

Embed
What would you like to do?
View helper generates html output for a Twitter Bootstrap styled control group

Twitter Bootstrap control_container method

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

def control_container(object, data_field, options = {})
#-------------------------------------------------------------------------------------------------
# Twitter Bootstrap styled control-group
# See help file at: https://gist.github.com/2505143 by Matt Slay
#-------------------------------------------------------------------------------------------------
#-- Parse the parameters ------------------------------
object = object.class.to_s.split("::").last.underscore
field_name = data_field.to_s
label_text = options[:label_text] ||= data_field.to_s.humanize()
help_text = options[:help_text] || ""
help_inline = options[:help_inline] ||= false
field_type = options[:field_type] ||= "text_field"
label = label_tag(object + "_" + field_name, label_text, :class => "control-label")
input = eval(field_type + "(object, field_name)")
if !help_text.empty?
help_class = "help-" + (help_inline == true ? "inline" : "block")
help = %{<span class="#{help_class}"><i>#{help_text}</i></span>}
end
html = %{
<div class="control-group">
#{label}
<div class="controls">
#{input}
#{help}
</div>
</div>
}
return html.html_safe
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.