【发布时间】:2017-05-30 15:18:15
【问题描述】:
我的 rails 应用程序使用 UJS 到处提交表单,但由于某种原因,我无法让它在这里工作。
我很确定我已经正确设置了 UJS(因为它在其他地方也可以使用):
#application.html.erb
<%= javascript_include_tag 'application' %>
#assets/javascripts/application.js
//= require jquery
//= require jquery_ujs
在我看来,我有一个form_tag 和一个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
谁能提出可能导致第二个无法按预期工作的原因?
【问题讨论】:
-
你试过
form_tag apply_tag_applications_path, method: :post, data: {remote: true}吗? -
使用
data: {remote: true}而不是普通的remote: true仍然不能解决问题,但是我注意到在使用您的建议 (data: {remote: true}) 呈现的 HTML 中导致了一个具有真实性的隐藏字段要添加到表单的令牌。根据我上面的问题,我没有看到这个带有代码的隐藏字段。 -
发布你的路由和控制器代码,以及显示相应的js文件的名称(应该是控制器的动作名称是.js.erb)
标签: ruby-on-rails ujs