I am using rails 4 to make an app like craigslist. I have a list of items on my index page. I am trying to create a dropdown select with options 'price', 'Most recent' so that users can sort the items based on these. Now, i need to send an ajax request to my '/items' url with params 'price' or 'most recent'. I have tried a lot of things but none have worked so far and i am completely stuck. This is the latest code i have. Are there any UBJ parameters i can use to accomplish this? I am new to rails and especially ajax, any help would be appreciated? Thanks in advance.
<script>
$('#sort_by').on('change',function()
{
$.ajax({ type: "GET",
url: "/items",
data: {sort: $(this).val();},
return false
})
}
</script>
Try this
$('#sort_by').on('change', function(){
$.ajax({ type: "GET",
url: "/items",
data: { sort: $('option:selected', this).val() }
}).done(function(data) {
console.log(data);
});
});
$('#sort_by').on('change', function(){
$.ajax({ type: "GET",
url: "/items",
data: { sort: $('option:selected', this).val() },
dataType : "script"
}).done(function(data) {
console.log(data);
});
});