Page Specific Javascript in Rails
UPDATE: I’ve written an updated article on this and the larger topic of organizing Javascript within a Rails project that includes Turbolinks.
The asset pipeline has made including javascript in Rails applications a breeze. Without any additional changes, the code included in your app is executed on every page. However, with a combination of CSS class scopes and a jQuery plugin, you can isolate certain javascript code to only run on specific pages.
The Problem
Imagine you have 2 pages in your application (landing and contact) and need an alert box to show up on the contact page only. How would you do it?
continue reading →