Hi all, I’m trying to overload the link_to function, (to disable link_to
if
the user has no access right)
this is my code, it work the first time I run the application, the
second
time I refresh the page I always get “stack level too deep error”
module UsersHelper
include ActionView::Helpers::UrlHelper
alias_method :link_to_original, :link_to
def permission?
true
end
def link_to(name, options = {}, html_options =
nil,*parameters_for_method_reference)
if permission?
link_to_original( name,
options,html_options,*parameters_for_method_reference )
end
end
end
the error message
howing app/views/users/_table.rhtml where line #40 raised:
I think when you call link_to_original in the link_to method, it calls
the link_to method again because link_to_original is aliased to link_to.
Alias does not make a ‘copy’ of the original function. You’re just
creating another name for it.
Why do you not just use link_to_if or link_to_unless … the first
parameter is a condition. If it fails, the link just becomes limp and
harmless text.
I’ve had a similar problem. What you have to realize is that the
alias_method call isn’t like declaring something at the class level in a
java-type language. It’s a call to a class method, and it can occur
more than once.
Your alias_method call is getting called twice. the first time, it
aliases link_to_original to the standard link_to method, since you have
not yet overridden linkto. You then override link_to. So far so good.
However, when alias_method gets called the second time (for whatever
reason), it aliases link_to_original to the current def of link_to,
which is now your overridden version. When link_to_original
subsequently gets referenced in your new link_to, it points to your new
link_to, creating an infinite loop.
Why is your class def being parsed twice? I dunno.