Rails "remote: true"适用于link_to,不适用于form_tag



我的Rails应用程序使用UJS在各处提交表格,但是由于某种原因,我无法在这里工作。

我很确定我已经正确设置了UJS(因为它在其他地方起作用(:

#application.html.erb
<%= javascript_include_tag 'application' %>
#assets/javascripts/application.js
//= require jquery
//= require jquery_ujs

在我看来,我既有 form_tag and a link_to,都设置为 remote: true

<%= form_tag apply_tag_applications_path, remote: true, method: :post do %>
  <%= submit_tag "FORM TAG SUBMIT" %>
<% end %>
<%= link_to apply_tag_applications_path, remote: true, method: :post do %>
  <h1>LINK TO SUBMIT</h1>
<% end %> 

单击link_to按预期工作(请求格式为JS(:

Started POST "/tag_applications/apply" for ::1 at 2017-05-30 11:10:51 -0400
Processing by TagApplicationsController#apply as JS

但是单击form_tag通过HTML提交:

Started POST "/tag_applications/apply" for ::1 at 2017-05-30 11:11:02 -0400
Processing by TagApplicationsController#apply as HTML

谁能建议什么可能导致第二个不按预期工作?

验证您是否在布局或资产中引用jquery_ujs文件。

#application.html.erb
<%= javascript_include_tag :jquery, :jquery_ujs %>
#assets/javascripts/application.js
require jquery.js
require jquery_ujs

另外,远程选项只有在您传递对象而不是通往form_for的路径时才能起作用。

这将起作用

<%= form_for @apply_tag, remote: true, method: :post do |f| %>
  <%= f.submit "FORM TAG SUBMIT" %>
<% end %>

最新更新