Search code examples
ruby-on-railsujs

App not executing js code in assets/javascripts


I've got a simple list with filter, and I want to clear the filter upon clicking clear (which also refreshes the list with all items).

I've got a form in index.html.erb:

</br >
<%= form_tag '', method: :get, remote: true do %>
  <%= label_tag 'content:' %>
  <%= text_field_tag :content %>
  <%= label_tag 'submitter:' %>
  <%= text_field_tag :submitter %>
  <%= label_tag 'feeback type:' %>
  <%= text_field_tag :feedback_type %>
  <%= submit_tag 'Filter', class: 'btn btn-primary' %>
  <%= link_to 'clear fields', feedback_messages_path, remote: true, type: 'button', class: 'btn btn-danger', id: 'clear-button' %>
<% end %>

In app/assets/feedback_messages, I've got:

$("#clear-button").click(function() {
  console.log('foo');
  $("#content").val(' ');
  $("#submitter").val(' ');
  $("#feedback_type").val(' ');
});

In application.js, I've tried every combination of require statements, but none work. Here's what I've got now, trying to explicitly load the js file:

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require turbolinks
//= require feedback_messages

When I load the webpage, the js file is available in sources, but will not execute. If I move the js to index.html.erb in a script tag, it works, so I don't think there's a problem with the js.

The code works appropriately with clearing lists, so I'm excluding the controller and js.erb code.


Solution

  • Pull remote: true from:

    <%= link_to 'clear fields', feedback_messages_path, remote: true, type: 'button', class: 'btn btn-danger', id: 'clear-button' %>
    

    try it and let me know what happens.