Search code examples
jqueryruby-on-railsturbolinks

Why is jQuery not working in my rails 5 app?


I am trying to write my first js code in my new Rails5 app and cannot seem to get Jquery to work, guessing it's a config/environment issue but despite all my searching and confirg alterations I cannot seem to get it running. Can anyone see what it is that I'm doing wrong?:

gemfile;

 # Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'

application.html.erb;

<title>MyEngine</title>
<!-- Gon::Base.render_data -->
<%= Gon::Base.render_data %>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

new.html.erb;

<%= link_to 'Remove Employee', '#', id: 'hide-employee' %>

quotes.coffee;

$(document).on "page:change", ->
    $('#hide-employee').click ->
        alert "Clicked!"

And no alert when clicked. No errors, server running and page rendering fine, just nothing in console.

Thanks for the help.


Solution

  • Try using turbolinks:load instead.

    $(document).on 'turbolinks:load', ->
      $('#hide-employee').click ->
        alert 'Clicked!'