Example for comment on openmonkey.com article "Rails Label Helpers with Blocks".
Created
April 7, 2010 13:00
-
-
Save timriley/358844 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% form_for(@article, :builder => SmartLabelFormBuilder) do |form| %> | |
<% form.label(:title, :text => 'Article Title') do %> | |
<%= form.text_field(:title) %> | |
<% end %> | |
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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