Attempt to override the 'tag' method

I want to override the ‘tag’ method in
ActionView::Helpers::TagHelper to do some generalized
error handling similar to the way scaffolding puts a
red border around fields that fail validation.
I’ve created a file lib/rails_patches/tag_helper.rb
which contains the following.

module ActionView
module Helpers
module TagHelper
alias_method :orig_tag, :tag
def tag(name, options = nil, open = false)
breakpoint
orig_tag(name, options, open)
end
end
end
end

I then have a view with the following

New post

<%= start_form_tag :action => 'create' %>

Title
<%= text_field 'post', 'title' %>

<%= submit_tag “Create” %>
<%= end_form_tag %>

Now here is my problem. Each of these tag helpers
calls the tag method to created it’s tag. However,
only some of them use my custom tag method. Here’s the
output. Notice that text_field does not use my
customer tag method. Why is that?

Executing break point at
/Users/jay/demo/public/…/config/…/lib/rails_patches/tag_helper.rb:6
in `tag’
irb(#<#Class:0x2578980:0x2578958>):001:0> name
=> :form
irb(#<#Class:0x2578980:0x2578958>):002:0> options
=> {“action”=>“/post/create”, “method”=>“post”}
irb(#<#Class:0x2578980:0x2578958>):003:0> exit

Executing break point at
/Users/jay/demo/public/…/config/…/lib/rails_patches/tag_helper.rb:6
in `tag’
irb(#<#Class:0x2578980:0x2578958>):001:0> name
=> :input
irb(#<#Class:0x2578980:0x2578958>):002:0> options
=> {“name”=>“commit”, “type”=>“submit”,
“value”=>“Create”}
irb(#<#Class:0x2578980:0x2578958>):003:0> exit


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

Hello Jay,

Now here is my problem. Each of these tag helpers
calls the tag method to created it’s tag. However,
only some of them use my custom tag method. Here’s the
output. Notice that text_field does not use my
customer tag method. Why is that?

text_field will return an InstanceTag object because it is bound
to a specific object (an AR::B object in most cases) so it doesn’t
seem to call your tag method. So the simplest way will be to use
text_field_tag() instead.

The problem will be the same with password_field, hidden_field,
file_field, text_area, check_box, radio_button… (it will work
with password_field_tag, etc.)

 -- Jean-François.

Jay D. wrote:

  alias_method :orig_tag, :tag

New post

only some of them use my custom tag method. Here's the output. Notice that text_field does not use my customer tag method. Why is that?

Active Record form helpers use the version of the tag helper
that is redefined in the InstanceTag class, with the original
tag function aliased as tag_without_error_wrapping.

You can redefine this version of tag by:

class ActionView::Helpers::InstanceTag
alias_method :orig_tag, :tag
def tag(name, options)
breakpoint
orig_tag(name, options)
end
end


We develop, watch us RoR, in numbers too big to ignore.