Saqib Shahzad
Saqib Shahzad

Reputation: 1002

Using same form as local and remote both in rails for filtering purposes

Scenario:

I have a form that does some filtering. For the sake of simplicity, let's assume I have a form that has three input options:

<%= form_tag(some_path,method: :get) do %>
     #..checkboxes for option 1
     #..radiobuttons for option 2
     #..checkboxes for option 3
     <%= submit_tag "submit" %>
<% end %>
<p>You have a total of: COUNT results.</p>

Required Output:

What I want is the functionality when a user clicks on any checkbox or radio button, (essentially a change in any input field), by ajax request should be generated to a path that returns a COUNT of total results, and I will update the COUNT inside p tag with that returned count number.

And when the user clicks on submit button, the default GET request should be generated.

Upvotes: 0

Views: 41

Answers (1)

Saqib Shahzad
Saqib Shahzad

Reputation: 1002

I added this script for ajax request, and it is working perfectly fine.

<script>
$(".option").change(function(){
    var type = $("input[name='type[]']:checked").map(function () {
        return this.value;
    }).get();

    $.ajax({
        url: "/gre",
        type: "put",
        dataType: "json",
        data: {custom: 'true',type: type},
        success: function (response) {
          var count = response["count"];
          $('#count').html('Your session will have a total of '+ count + ' questions.');
        }
    });
});

Upvotes: 1

Related Questions