0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you explain why this works asynchronously without the remote: true param? Is it because it's within the form tag?
The $("#clear-button").click(function() is operating on the form. So the link "#clear-button" having remote true tells rails that some action was to take place back on the server. I'm not 100% sure but I believe the JavaScript that controls remote true may have swallowed the click function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.