With Rails 3+ and the asset pipeline my understanding is that all the javascript files under assets/javascripts get automatically included and compiled into one js file if you specify
require_tree .
in application.js.
What is the best practice regarding dealing with javascript that is specific to a controller/view? For example suppose I have a controller/view 'Houses' and I want to validate the houses form using the jquery form validation plugin, to do that I have placed the following code in my houses.js file
$(document).ready(function(){
$("#new_house_form").validate();
});
which is fine but the problem is that when I visit another page (say /owners), it tries to validate the houses form and I get an error message in the javascript console. Ofcourse I could just check if the form exists on the current page or not and only then call the validator but that seems a little unclean to me esp if I have a lot of controller specific code in .ready.
My solution to work around this has been to create a folder assets/javascripts/global and in application.js
require_tree ./global //instead of require_tree .
and move all the other js files except for houses.js in it. Then I manually include houses.js whenever the user accesses /houses/new or /houses/edit
Is this practice acceptable? If not what is considered to be the best practice to deal with these situations?