动态嵌套表单 link_to_add 被调用两次
我正在使用 ryan bates Nested_form gem 动态地将一些嵌套字段添加到表单中。
例如
<%= f.fields_for :phones do |phone_form| %>
<%= phone_form.text_field :phone_number %>
<% end %>
<%= f.link_to_add "Add a phone", :phones %></p>
,一切正常,除了每次单击链接时都会添加两个空字段。
我在 $('form a.add_nested_fields').live('click', function()
上放置了一个断点,看到它被调用了两次......
我在 mac 上使用 chrome
I am using ryan bates nested_form gem to dynamically add some nested field to a form.
e.g.
<%= f.fields_for :phones do |phone_form| %>
<%= phone_form.text_field :phone_number %>
<% end %>
<%= f.link_to_add "Add a phone", :phones %></p>
Everything works, except TWO empty fields are added every time the link is clicked.
I placed a breakpoint on $('form a.add_nested_fields').live('click', function()
and see it is called twice...
I am using chrome on a mac
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看您的标题。您会看到它在那里两次:
只需删除第二个引用(可能在您的 application.html.erb 中),它就会起作用。
Look in your header. You'll see that this is in there twice:
Just remove the second reference (perhaps in your application.html.erb) and it will work.
您可能有嵌套表单 js 文件被加载两次
you may have nested form js files being loaded twice
对于那些使用 Rails 4 / Turbolinks 遇到此问题并且未找到两次包含nested_form.js 的人,请尝试从 application.js 中删除 Turbolinks。一旦我这样做并退回服务器,这个问题就解决了。
来源:https://github.com/ryanb/nested_form/issues/307
For those having this issue using rails 4 / turbolinks and not finding nested_form.js included twice, try removing turbolinks from application.js. Once I did this and bounced the server this issue was resolved.
Credit here: https://github.com/ryanb/nested_form/issues/307
检查是否加载多个 js 文件,如果您在多个项目中工作,请确保清除浏览器缓存
check for loading js files more than one and make sure to clear you browser cache if you work in multiply projects