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 %>
2
  • You sure you have JQuery running & added to your pages? Commented Dec 23, 2013 at 12:49
  • Use proper button id and proper ajax calling with jquery [Like here][1] [1]: stackoverflow.com/questions/10406571/… Commented Dec 23, 2013 at 12:53

2 Answers 2

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

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

4 Comments

should I use href in the attr("href"); or I should use the link there?
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
It is working fine for link but not working for button. Please check the question now
Okay, you have to remember that button_to basically creates a form, so you'd have to do something like url: $(this).parent().attr("action");
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();
    });
});

2 Comments

It is working fine for link but not working for button. Please check the question now
Why do you have to use the button_to helper? <button type="button" class="line-item" product="#{product.id}"><%= t('.add_html') %></button>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.