An extension of an extension of Paul Irish’s Dom-Ready Execution
Page specific bindings in rails can be achieved in many different ways. Recently I came across this post by @brandonhilkert and it inspired me to post about the solution that I have been using for awhile.
Now it is important to note that I did not come up with what I currently use on my own. It all started with a solution that @paul_irish had posted in 2009 here and then was extended to be even more awesome by @jgarber here. With all of the creds handed out appropriately, I’ll show how a colleague @mtrpcic and I were able to extend @jgarber’s solution even further.
So alike @jgarber and @paul_irish’s solution the first thing required is to use the rails controller_name and action_name helper as data attributes on the body.
<body data-controller=“<%= controller_name %>” data-action=“<%= action_name %>” >
Once all of this is set up, you’re ready to define your application object.
And lastly your controllers. For the sake of the example, I’ll stick with Brandon Hilkert’s example of running an alert on Pages#contact action.
Then in my manifest file, after everything is included, I initialize the app object.
//= require jquery //= require jquery_ujs //= require turbolinks //= require_tree app.init();