0

I have an Ajax request in my Rails 4 app, but once the button is clicked it does not add the data to the DB. What am I missing? The page is cached so I cannot simply add a link_to or button_to. Thanks in advance.

JS File:

$(document).ready(function() {

 $(".listcollect").on("click", function(event) {
    event.preventDefault();
    var listID = this.id;
    $.ajax({
        method: "GET",
        url: "/listing_collections/:id/listcollect",
        success: function(result) {
            var arrLen = result.length

            for (var i = 0; i < arrLen; i++) {
                var $li = $("<li></li>");
                var $a = $('<a class="listing-collection-item"></a>');
                $a.attr("href", "/listings/" + listID + "/add_to_collection?listing=" + listID + "$amp;listing_collection_id=" + result[i]["id"] + "&amp;listing_id=" + listID)
                $a.text(result[i]["name"])
                $li.append($a)
                $(this).next().append($li)
            }
        }.bind(this)
    })
});

$("ul.dropdown-menu").on("click", ".listing-collection-item", function(e) {
    var url = $(this).attr("href");
    alert("The listing was added to the collection!");

    return false;
   });
});

My html.erb file:

<div class="btn-group listcollect-wrapper">
    <button class="listcollect button btn-blue-white btn-2x dropdown-toggle" data-toggle='dropdown' type="button" id=<%= listing.id %>>Add to Listing Collection <span class="caret"></span></button>
    <ul class='dropdown-menu'>

    </ul>
</div>
Mike Wiesenhart
  • 316
  • 1
  • 4
  • 19

1 Answers1

1

As far as I can tell you aren't sending any data to your script.

You'll need to add some sort of data to send.

Example

$.ajax({
  method: "GET",
  url: YOUR_URL,
  data: {
    data1: "value" 
  },
  success: function(){},
  ...
});

See this answer for additional details. https://stackoverflow.com/a/22179776/2370075

Community
  • 1
  • 1
Greg McMullen
  • 989
  • 2
  • 16
  • 28