Skip to content

Instantly share code, notes, and snippets.

@lisaSwanson
Forked from stujo/application.html.erb
Created October 1, 2015 17:27
Show Gist options
  • Save lisaSwanson/9784ae1770f61c8ffcab to your computer and use it in GitHub Desktop.
Save lisaSwanson/9784ae1770f61c8ffcab to your computer and use it in GitHub Desktop.
Page Specific Document Ready JavaScript in Rails with jQuery
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PizzaShack</title>
<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>
<div class="container">
<%= yield %>
</div>
</body>
<script>
// THIS IS THE IMPORTANT PART
var event_name = 'pizza:<%= controller_name %>_<%= action_name %>';
$.event.trigger({ type: event_name});
</script>
</html>
$(document).on("pizza:pizzas_index", function(){
alert('Hello on the Pizzas Page');
})
$(document).on("pizza:welcome_index", function(){
alert('Hello on the Welcome Index Page');
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment