1

I wrote a sample program to fetch all articles. I would like to filter articles based on a category. The categories are rendered in a drop down, how do I fire a AJAX query when I select a category, so that the table refreshes immediately with that selection. Can you provide some references on how to achieve this?

select * from articles where category = <drop down selection>
1

2 Answers 2

1

Try writing as following:

<%= select_tag :category,options_for_select(:your_collection)%>


 $('#category').bind('change', function() {
  $.ajax({
   url: your_controller_action,
   data : {category: $('#category').val()}
   success: function(data){                  

        $('#your_replace_div_id').html(data);
        }        

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

Comments

0

use js to get the category_id $('#category').options[$('#category').options.selectedIndex].value

and then post this value to server by ajax

at server side, you can pass this category_id value into sql select * from articles where category = "#{category_id}"

Comments

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.