Forum: Ruby on Rails attempt to override the 'tag' method

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8de6094e9df399c87da91be9ddf963e5?d=identicon&s=25 Jay Donnell (bloc)
on 2006-05-19 00:17
(Received via mailing list)
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

<h1>New post</h1>
<%= start_form_tag :action => 'create' %>

  <p><label for="post_title">Title</label><br/>
  <%= text_field 'post', 'title'  %></p>

  <%= 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
http://mail.yahoo.com
13ea48f056b19349027d2f4a6921a46a?d=identicon&s=25 Jean-François (Guest)
on 2006-05-19 02:25
(Received via mailing list)
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.
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2006-05-19 02:55
(Received via mailing list)
Jay Donnell wrote:
>       alias_method :orig_tag, :tag
> <h1>New post</h1>
> 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.
This topic is locked and can not be replied to.