Code examples for openmonkey.com article "Rails Label Helpers with Blocks".
Created
March 30, 2010 03:00
-
-
Save timriley/348707 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) do %> | |
Title | |
<%= 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 shit. | |
options = content_or_options_with_block.is_a?(Hash) ? content_or_options_with_block.stringify_keys : {} | |
if errors_on?(method) | |
(options['class'] = options['class'].to_s + ' error').strip! | |
end | |
@template.content_tag(:label, options, &block) | |
end | |
end | |
private | |
def errors_on?(method) | |
@object.respond_to?(:errors) && @object.errors.respond_to?(:on) && @object.errors.on(method.to_sym) | |
end | |
end | |
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance_tag| | |
html_tag | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment