0

In my rails application I have this code

<a data-remote="true" href="#" class: "find_product">Find product</a>

For ajax call I am doing this

$(document).on('click', '.find_product', function(){     
              $.ajax({
                     url: "<%= startup_wizard_find_horse_path() %>",
                    dataType: 'script',
                    type: 'GET',
                    data: { box_no: "3"}
            });
            return false;


        });

Now when I click on the above link ajax call is sent to the current page action not to the

startup_wizard_find_horse_path

Why this is happening and how can I make this work?

asdfkjasdfjk
  • 3,784
  • 18
  • 64
  • 104
  • I might not know Rails, so at the risk of getting flogged: WTF is `class: "find_product"`? – MonkeyZeus Apr 09 '14 at 14:34
  • And one more thing, learn to use the **[Network Tab](http://stackoverflow.com/a/21617685/2191572)** if you want to avoid hours of hair-pulling – MonkeyZeus Apr 09 '14 at 14:36

1 Answers1

0

If you're going to use data-remote, set the href value and don't define a click event, that's the point of unobtrusive JS in Rails.

If you want to use jQuery manually, remove the remote = true and change your jQuery click to this, to disable the default behavior of the link:

$(document).on('click', '.find_product', function(event) {
  event.preventDefault();
  $.ajax({
    url: "<%= startup_wizard_find_horse_path() %>",
    dataType: 'script',
    type: 'GET',
    data: { box_no: "3"}
  });
  return false;
});

EDIT: Here's a post that recaps how to use remote links: link_to and remote => true + jquery : How? Help?

Community
  • 1
  • 1
tirdadc
  • 4,603
  • 3
  • 38
  • 45
  • when I remove this part data: { box_no: "3"} from ajax call this work just fine – asdfkjasdfjk Apr 09 '14 at 12:38
  • You're still mixing two different approaches, your remote link is pointless if you're not defining the URL and writing custom jQuery. – tirdadc Apr 09 '14 at 12:42
  • What error do you get in your JS console? in your Rails log? Is the URL rendered properly in your JS output? What does your `startup_wizard_find_horse` method look like? – tirdadc Apr 09 '14 at 12:49