Skip to content

Instantly share code, notes, and snippets.

@timriley
Created April 7, 2010 13:00
Show Gist options
  • Save timriley/358844 to your computer and use it in GitHub Desktop.
Save timriley/358844 to your computer and use it in GitHub Desktop.
<% form_for(@article, :builder => SmartLabelFormBuilder) do |form| %>
<% form.label(:title, :text => 'Article Title') do %>
<%= form.text_field(:title) %>
<% end %>
<% end %>
class SmartLabelFormBuilder < ActionView::Helpers::FormBuilder
def label(method, content_or_options_with_block = nil, options = {}, &block)
if !block_given?
# No block, use the standard label helper.
super(method, content_or_options_with_block, options)
else
# We've got a block. This is where we want to do custom stuff.
options = content_or_options_with_block.is_a?(Hash) ? content_or_options_with_block.stringify_keys : {}
span_text = options.delete('text')
@template.concat(@template.content_tag(:label, options) {
@template.content_tag(:span, span_text) + @template.capture(&block)
})
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment