1

I am doing a small rails app.

I am trying to make an ajax call. Despite I use remote: true the button_to is making a non ajax request. It redirects me to this url http://localhost:3000/lines?product_id=1

<% @products.each do |product| %>
  <%= link_to product.name, controller: "store", action: "show", id: product %><br>
  <%= button_to "Add to Cart", {controller: "lines", action: "create", product_id: product.id}, method: "POST", remote: true %>
<% end %>

github repo

Any idea?

Thanks!

---EDIT--- this was my application.js

= require jquery
= require jquery_ujs
//= require turbolinks
//= require_tree .
Gerard Morera
  • 799
  • 6
  • 12

1 Answers1

2

Issues like this are usually because you aren't loading jquery correctly. The button_to ... remote: true becomes an ajax call because the remote: true triggers javascript that changes the behaviour of the button.

In your application.js you are requiring javascript with:

= require jquery
= require jquery_ujs

This should be:

//= require jquery
//= require jquery_ujs

With jquery loaded correctly you should get the button behaving as expected.

Shadwell
  • 34,314
  • 14
  • 94
  • 99
  • Thanks @Shadwell! You are right. Why is it wrong to require jquery? – Gerard Morera Sep 25 '15 at 11:24
  • 1
    It's not wrong to require it it's the way it was being required. The lines really do need that "commented out" format as that is what the asset pipeline expects. (I don't think you need the `//= require jquery` as `jquery_ujs` includes it. – Shadwell Sep 25 '15 at 11:38