Submit button outside form_for loop

送分小仙女□ 提交于 2020-01-05 11:35:13

问题


I have set up some horizontal tabs using twitter bootstrap and I am rendering a form inside each of the tabs:

<div class="tab-content">
  <div id="tab1" class="tab-pane active">
      <%= render :partial => "shipdr/websites/form", locals: {:@shipdr_website => shipdr_website} %>
  </div>
  <div id="tab2" class="tab-pane">
    Another form (not yet implemented)
  </div>
  <div id="tab3" class="tab-pane">
    Another form (not yet implemented).
  </div>
</div>

Then in shipdr/websites/form I have:

<%= simple_form_for(@shipdr_website) do |f| %>
  <% if @shipdr_website.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@shipdr_website.errors.count, "error") %> prohibited this shipdr_website from being saved:</h2>

      <ul>
      <% @shipdr_website.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.input :name %>
  </div>
  <div class="field">
    <%= f.input :url %>
  </div>
  <div class="field">
    <%= f.input :api_key %>
  </div>
  <div class="actions">
    <%= f.submit nil, :class => "btn btn-primary" %>
  </div>
<% end %>

I want to move the submit button outside of the "tab-content" area so when a user clicks the submit button, all three forms area submitted. All forms will use that same model and the same action but will have different field. The idea is similar to a wizard except I am using tabs.

Does anyone have any idea how I can move the submit button outside of the form_for loop and how I can submit the three forms with a single button?


回答1:


I don't know if you can submit 3 forms at a time. But you can do this:

<div class="tab-content">
  <%= simple_form_for(@shipdr_website) do |f| %>
    <div id="tab1" class="tab-pane active">
      <%= render :partial => "shipdr/websites/form", locals => {:f => f, :shipdr_website => @shipdr_website} %>
    </div>
    <div id="tab2" class="tab-pane">
      Another form (not yet implemented)
    </div>
    <div id="tab3" class="tab-pane">
      Another form (not yet implemented).
    </div>
  <% end %>
</div>

And remove that from your partials, leaving just the inputs.

<%= simple_form_for(@shipdr_website) do |f| %>



回答2:


I think the best way of doing that is with Javascript: you can move the button and send 3 forms at a time via Ajax. If you use Jquery you can use http://jquery.malsup.com/form



来源:https://stackoverflow.com/questions/11147435/submit-button-outside-form-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!