2017-05-30 119 views
0

我的Rails应用程序使用UJS在各处提交表单,但由于某种原因,我无法在此处使用它。Rails“remote:true”适用于link_to,不适用于form_tag

我确信我有UJS正确设置(因为它在其他地方工作):

#application.html.erb 
<%= javascript_include_tag 'application' %> 

#assets/javascripts/application.js 
//= require jquery 
//= require jquery_ujs 

在我看来,我同时拥有form_taglink_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 

但是通过点击HTML的form_tag的提交:

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

任何人都可以提出什么可能导致第二个没有达到预期效果?

+0

您是否尝试过'的form_tag apply_tag_applications_path,方法:后,数据:{远程:真正}'? –

+0

使用'data:{remote:true}'而不是普通的'remote:true'仍然没有解决这个问题,但是我注意到使用你的建议呈现的HTML('data:{remote:true}')导致带有真实性标记的隐藏字段被添加到表单中。我没有看到这个隐藏的领域的代码,根据我上面的排队。 –

+0

张贴您的路线和控制器代码,以及显示相应的js文件的名称(应该是无论控制器的行动名称是什么is.js.erb) – the12

回答

0

确认您在布局或资产中引用了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 %> 
+0

是的,我可以验证这两个引用是否到位(编辑问题以反映这一点)。 –

+0

我编辑了答案。基本上,当你传递一个对象而不是一个路径时,远程选项将起作用。或者你也可以使用url选项发送。 –

+0

对不起,这仍然不起作用。以HTML格式收到请求: '在2017-05-30 11:52:23 -0400处启动POST“/ tag_applications”:: '通过TagApplicationsController处理#以HTML创建# –

相关问题