如何在 Rails 中远程提交表单的相关部分而不提交整个表单?
我有一个简单的 Rails 表单,允许编辑其关联的父表单。我想允许用户使用 :remote => 提交表单的这一部分true 以便用户可以添加新的父级,然后在表单中其他位置的更新选择菜单中选择父级。正如您在代码中看到的,我在表单的父级部分添加了一个提交按钮,它甚至知道是说“创建”还是“更新”,但是当我提交它时,整个页面都会刷新,整个页面都会刷新。提交表单进行验证等。我怎样才能在 Rails 中完成我想要的事情?
这是有问题的代码:
<%= form_for @sermon, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.text_field :date %>
</div>
<div class="field">
<%= f.label "Speaker" %><br />
<%= f.select :speaker_id, Speaker.all.collect {|p| [ p.name, p.id ] }, {:include_blank => true} %>
</div>
<% @sermon.build_speaker unless @sermon.speaker %>
<%= f.fields_for :speaker, :remote => true, :html => {:data_type => 'html', :id => 'create_speaker_form'} do |g| %>
<%= g.label :name, "Or, add a new speaker:" %><br />
<%= g.text_field :name %>
<%= g.submit %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I have a simple Rails form that allows for its associated parent to be edited. I would like to allow the user to submit this section of the form using :remote => true so that the user can add a new parent, and then select the parent in an updated select menu elsewhere in the form. As you can see in the code, I added a submit button to the parents' part of the form, and it even knows whether to say "Create" or "Update," but when I submit it the entire page is refreshed, the entire form is submitted for validation and so forth. How can I accomplish what I want in Rails?
Here is the code in question:
<%= form_for @sermon, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :date %><br />
<%= f.text_field :date %>
</div>
<div class="field">
<%= f.label "Speaker" %><br />
<%= f.select :speaker_id, Speaker.all.collect {|p| [ p.name, p.id ] }, {:include_blank => true} %>
</div>
<% @sermon.build_speaker unless @sermon.speaker %>
<%= f.fields_for :speaker, :remote => true, :html => {:data_type => 'html', :id => 'create_speaker_form'} do |g| %>
<%= g.label :name, "Or, add a new speaker:" %><br />
<%= g.text_field :name %>
<%= g.submit %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能您的问题已在这里得到解答 如何您是否在不刷新页面的情况下提交 Rails 3 表单?
如果这没有帮助,您可以尝试使用 preventDefault() 将其附加到您的提交按钮。
probably your question has been answered here How do you submit a rails 3 form without refreshing the page?
if that doesn't help you can try using preventDefault() attaching it to your submit button.