Remote true on button_to not进行ajax调用

remote true on button_to not makes ajax call

本文关键字:not 进行 ajax 调用 to true on button Remote      更新时间:2023-09-26

我正在做一个小的rails应用程序。

我正在尝试进行ajax调用。尽管我使用remote: true的button_to是一个非ajax请求。它重定向到这个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回购

任何想法?

谢谢!

——编辑这是我的application.js

= require jquery
= require jquery_ujs
//= require turbolinks
//= require_tree .

这样的问题通常是因为你没有正确加载jquery。button_to ... remote: true变成ajax调用,因为remote: true触发javascript,改变按钮的行为。

在你的application.js中,你需要javascript:

= require jquery
= require jquery_ujs

应该是:

//= require jquery
//= require jquery_ujs

正确加载jquery后,您应该会得到按钮的行为如预期的