【问题标题】:How do I submit the associated section of a form remotely in Rails without submitting the entire form?如何在 Rails 中远程提交表单的相关部分而不提交整个表单?
【发布时间】:2012-02-22 04:58:03
【问题描述】:

我有一个简单的 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 %>

【问题讨论】:

    标签: ruby-on-rails ruby ajax activerecord erb


    【解决方案1】:

    您的问题可能已经在这里得到解答How do you submit a rails 3 form without refreshing the page?

    如果这没有帮助,您可以尝试使用 preventDefault() 将其附加到您的提交按钮。

    【讨论】:

    • 这不是真正的问题——我在尝试提交的主表单中有一个关联的表单。
    猜你喜欢
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多