In my application, any time there is a form, I call two helper methods to inject some javascript into the head of the web page. One method injects code to focus on the first input of the form and the second method hooks up some call backs that I use with the client_side_validation gem.
I will be calling these two methods on any page that has a form_for in the view. Is there a way to automatically invoke these methods any time form_for is called as opposed to adding the calls to my template files?
Using Rails 3.1.
You should wrap form_for with your own method that calls your helpers. Fortunately Rails provides an easy way of doing this in the form of
alias_method_chain.You can now call form_for_without_my_helpers with the normal form_for arguments if you don’t want a form to use your helpers.
This can go somewhere that’s in the load path. Or you can require the file holding this code.