单击 form_for 中的提交按钮会在我的数据库中生成 3 项
我的 Rails 应用程序中有一个表单,它在我的数据库中创建一个项目,但是当我提交表单时,它每次点击都会创建 3 个项目。
我有另外 2 个表单将内容添加到同一数据库,但它们位于不同的页面上,这可能相关吗?
这是我在“新辩论”页面上的表单:
<%= form_for(@debate) do |f| %>
<div class="field">
<%= f.label :proposition %><br />
<%= f.text_field :proposition %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
我在“显示辩论”页面上还有另一个表单,该表单出现两次:
<%= form_for(@debate.debates.create) do |support_form| %>
<div>
<%= support_form.label :content %><br />
<%= support_form.text_area :content %>
</div>
<%= support_form.hidden_field :is_supporting, :value => is_supporting %>
<div class="actions">
<%= support_form.submit %>
</div>
<% end %>
当我单击 3 个表单中任何一个表单上的提交按钮时,我会收到 3 个新辩论。
I have a form in my rails app that creates an item in my database, but when I submit the form, it creates 3 items per click.
I have 2 other forms that add things to the same database, but they are on different pages, could that be related?
This is my form on the "new debate" page:
<%= form_for(@debate) do |f| %>
<div class="field">
<%= f.label :proposition %><br />
<%= f.text_field :proposition %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I also have another form on the "show debate" page that appears twice:
<%= form_for(@debate.debates.create) do |support_form| %>
<div>
<%= support_form.label :content %><br />
<%= support_form.text_area :content %>
</div>
<%= support_form.hidden_field :is_supporting, :value => is_supporting %>
<div class="actions">
<%= support_form.submit %>
</div>
<% end %>
And when I click on the submit button on any of the 3 forms, I get 3 new debates.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你的代码可能会创建那些额外的记录。
如果我的假设是正确的,
.debates
是一个关联,并且您正在与该行创建该关联。尝试使用
构建
I think your code, might be creating those extra records.
If my assumption is correct
.debates
is an association, and you are creating that association with that line.Try using
build