Gerard Morera
Gerard Morera

Reputation: 859

remote true on button_to not makes ajax call

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 .

Upvotes: 1

Views: 645

Answers (1)

Shadwell
Shadwell

Reputation: 34774

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.

Upvotes: 2

Related Questions