1

This is working just fine

<%#= link_to t('.add_html'), 'javascript:void(0);', :class => "line-item", :product => product.id %>

$('document').ready(function(){
        $(".line-item").click(function(){
            var prod = $(this).attr('product');
            $.ajax({
                url:'<%#= line_items_url %>',
                data: {product_id: prod},
                type: 'POST',
                dataType: 'script'
            });
        });
    });

But when I use button nothing happens. Please let me know what am I missing here?

<%= button_to t('.add_html'), 'javascript:void(0);', :class => "line-item", :product => product.id %>
  • You sure you have JQuery running & added to your pages? – Richard Peck Dec 23 '13 at 12:49
  • Use proper button id and proper ajax calling with jquery [Like here][1] [1]: http://stackoverflow.com/questions/10406571/click-jquery-button-send-data-without-form-bookmark – Bharat soni Dec 23 '13 at 12:53

2 Answers2

0

:remote => :true just creates an ajax request; you can do your own ajax request no problem:

$("button").on("click", function(){
    $.ajax({
        url: $(this).attr("href");
        success: function(data) { //handle returned data },
        error: function(data)   { //handle errors }
    });
});

I think you are asking a different question (how to get your call working), which I can update the answer to reflect if you wish

Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • should I use href in the attr("href"); or I should use the link there? –  Dec 23 '13 at 13:40
  • You can use link! Only problem is I don't think your syntax is correct; but you literally just put your link in there. How you get it is irrelevant – Richard Peck Dec 23 '13 at 13:42
  • It is working fine for link but not working for button. Please check the question now –  Dec 23 '13 at 13:50
  • Okay, you have to remember that [`button_to` basically creates a form](http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-button_to), so you'd have to do something like `url: $(this).parent().attr("action");` – Richard Peck Dec 23 '13 at 18:19
0

You need to prevent default:

$(document).ready(function(){
    $("button").click(function(ev){
        $.post(this.url); // I'm not sure this is correct
        ev.preventDefault();
    });
});
Aqabawe
  • 339
  • 3
  • 7