In Ruby on Rails, a method defined in view cannot override a helper method?
I have a debug method that can print out some debug info, and this debug method is defined in application_helper.rb
To disable this method, so that no debug info i开发者_如何学Pythons printed for this file, I added
def debugp; return nil; end
in the view file that uses the original debugp
... but it seems that this new definition cannot override the original definition of debugp
(because debug info is still printed out). Is there a way to override it? There are other methods to disable the printing, but I'd like to find out the part about override a method in the application helper. Thanks.
You can't override the method in the view file, however you can override it in the corresponding helper - if you used the Rails generator to generate your controller, then it would have generated a helper too, for example, a "people" controller would have these files:
- app/controllers/people_controller.rb
- app/views/people/*.html.erb
- app/helpers/people_helper.rb
So to override a helper method from the application helper, put your helper method definition not in the view, but in the helper:
module PeopleHelper
def debugp; return nil; end
end
Hope that helps,
精彩评论