Martin Solli
2005-Dec-27  12:36 UTC
Re: RESOLVED: Overloading error_message_on method in ActiveRecordHelper
Leaving the body of the method the same as the default still throws
the "stack level too deep" error.  As a matter of fact, it only
disappears if I comment out the ''require'' in line 1.
As it turned out, it was the method definiton itself that caused
problems. Ruby doesn''t have keyword arguments, but I tried calling the
method as if it had. But when I changed the definiton to one with a
hash that simulates keywords, it works just fine:
---
module ActionView
  module Helpers
    module ActiveRecordHelper
      def error_message_on(object, method, params = nil)
        if params.class == Hash
          prepend_text = params[:prepend_text] || ""
          append_text  = params[:append_text]  || ""
          css_class    = params[:css_class]    || "formError"
          span         = params[:span]         || false
        end
        if errors =
instance_variable_get("@#{object}").errors.on(method)
          content_tag(span ? "span" : "div",
"#{prepend_text}#{errors.is_a?(Array) ? errors.first :
errors}#{append_text}", :class => css_class)
        end
      end
    end
  end
end
---
I don''t know if this is the proper way to do it, but it seems to work.
Now I can use
<%= error_message_on(''user'',
''first_name'', :span => true) %>
in my templates when I want my error messages in a <span> instead of in a
<div>.
-martin
On 12/23/05, David Lee <david-4y1Gq2eXLBXvnOemgxGiVw@public.gmane.org>
wrote:> I''m trying to do something quite similar myself for the first
time.
>
> i''ll offer one piece of advice: putting that code in a file that
also
> contains a model or helper means you don''t have to restart the
server
> to see your changes, which makes debugging the thing a lot easier
> (thats not a deployment strategy mind you).
>
> If you leave the body of your method the same as AR''s default does
it
> still throw the ''stack too deep'' ?
>
> cheers,
> David
>
>
> On 23/12/2005, at 11:51 PM, Martin Solli wrote:
>
> > Hello all
> >
> > I would like to overload the error_message_on method in the
> > ActiveRecordHelper module in order to emit a span tag instead of a div
> > tag. I try to achieve this by way of plugins: Under vendor/plugins
> > I''ve made a error_messages_on_fix directory, containing
init.rb like
> > this:
> > ---
> > require ''active_record_helper_fix''
> > ---
> >
> > ..and a lib directory with active_record_helper_fix.rb like this:
> > ---
> > require ''action_view/helpers/active_record_helper''
> >
> > module ActionView
> >   module Helpers
> >     module ActiveRecordHelper
> >       def error_message_on(object, method, prepend_text =
"",
> > append_text = "", css_class = "formError", span =
false)
> >         if errors =
instance_variable_get("@#{object}").errors.on
> > (method)
> >           content_tag(span ? "span" : "div",
> > "#{prepend_text}#{errors.is_a?(Array) ? errors.first :
> > errors}#{append_text}", :class => css_class)
> >         end
> >       end
> >     end
> >   end
> > end
> > ---
> >
> > My plan is to be able to use this method with a call like this:
> > error_message_on(''user'',
''last_name'', :span => true)
> >
> > This results in my application coming tumbling down with this error:
> > ---
> >  Showing app/views/user/_form.rhtml where line #3 raised:
> > stack level too deep
> > Extracted source (around line #3):
> > 1: <div class="user_first_name">
> > 2: <p><label
for="user_first_name">Fornavn</label><br />
> > 3: <%= text_field(''user'',
''first_name'', ''size'' => 25) %>
> > 4: <%= error_message_on(''user'',
''first_name'') %></p>
> > 5: </div>
> > ---
> >
> > I feel that the problem may be my lack of understanding of how modules
> > work in Ruby. Or I''m trying to do something simple in a hard
way. I
> > don''t know. I would appreciate some tips on how to achieve
this.
> >
> > -martin
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
