简体   繁体   English

HTML / Rails表单仅在重新加载时提交

[英]HTML/Rails form only submits on reload

I have a rails form to create up to 25 new objects (essays in this app) at once using a custom controller. 我有一个Rails表单,可以使用自定义控制器一次最多创建25个新对象(此应用中的文章)。 I've written some simple jquery that hides forms 2-25 until a user clicks the button to "Add More Essays," which reveals the rest of the forms. 我编写了一些简单的jquery,可隐藏2-25表格,直到用户单击“添加更多随笔”按钮,该表格将显示其余表格。 The submit/persistence works fine after refreshing the page - and even though I've hit this error before - I'm pretty sure it's an HTML issue. 刷新页面后,提交/持久性工作正常-即使我之前遇到过此错误-我也可以肯定这是HTML问题。 HTML is not my strength, and I can't find the source of the problem. HTML不是我的强项,我也找不到问题的根源。 I know forms/tables can be problematic when used together as per this post. 我知道,根据帖子一起使用表格/表格时可能会出现问题。 Any guidance would be great. 任何指导都会很棒。

HTML: HTML:

    <!-- essay form -->

<%= form_tag essays_path, multipart: true do |form| %>
  <% @essays.each do |essay| %>
   <% if essay.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@essay.errors.count, "error") %> prohibited this essay from being saved:</h2>

      <ul>
      <% @essay.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>  <!-- closes @essay.errors... loop -->
   <% end %>  <!-- closes essay.errors... loop -->
      </ul>
    </div>
  <% end %>  <!-- closes @essays.each... loop -->


  <% @essays.each do |essay| %>
    <%= fields_for 'essays[]', essay do |f| %>
    <div class="field list-group-item essay-upload">

      <div class="actions col-md-6 col-md-offset-3">
          <%= submit_tag 'Upload Essay(s)', :class => 'upload-individual-essay btn btn-lg btn-primary btn-block upload-new-essays'%>
      </div>

      <table class="table">

        <thead>
          <tr>
            <% if current_user.admin? %>
            <th>Company</th>
            <% end %>
            <th>Student First Name</th>
            <th>Student Last Name</th>
            <th>Package</th>
            <th>Document</th>
            <th></th>
          </tr>
        </thead>

        <tbody>
          <tr class="essays-new-table-row">
            <% if current_user.admin? %>
            <td>
            <div class="field list-group-item">
                <div>
                  <%= f.select :company_id, options_for_select(@companies) %>
                </div>
            </div>
            </td>
            <% end %> <!-- closes current_user.admin? -->

            <td>
              <%= f.text_field :student_first_name, class: 'form-control', placeholder: 'First Name' %>
            </td>
            <td>
              <%= f.text_field :student_last_name, class: 'form-control', placeholder: 'Last Name' %>
            </td>
            <td>
                <div class="list-group-item">
                  <%= f.select :package_id, options_for_select(@packages) %>
                </div>
            </td>
            <td>
              <%= f.file_field :document, class: "essays__document" %>
            </td>
          </tr>
        </tbody>

      </table>

      <button type="button" class="btn btn-lg btn-success btn-block add-another-essay"> Add More Essays</button><br>

    </div>  <!-- <div class="field list-group-item essay-upload">  -->

    <% end %>  <!-- closes field_for -->

  <% end %>  <!-- closes second @essays.each ... -->

<% end %>  <!-- closes closes form  -->

JS: JS:

var ready;
ready = function() {

  // FUNCTIONALITY: hide extra divs

  $(".essay-upload").hide();
  $(".essay-upload:first").show();

  // FUNCTIONALITY: reveal other forms on 'add more essays' click 

  $('.add-another-essay').on('click', function(){
    $(".essay-upload").show();  
    $('.add-another-essay').remove(); 
    $('.upload-individual-essay').hide();
    $('.upload-individual-essay:first').show();
  })

$(document).on('page:change', ready);

According to what I see you always show only only one (first) submit_tag , but you have 25! 据我所见,您始终只显示一个(第一个) submit_tag ,但是您只有25个! submit tags on the page... 在页面上提交标签...

What do you think about moving this submit_tag outside the loop? 您如何看待将此submit_tag移出循环? (@essays.each). (@ essays.each)。 It's not needed there (it submits top form). 那里不需要它(它提交顶部表单)。 In this case you can avoid hiding & showing submit tag 在这种情况下,您可以避免隐藏并显示提交标签

$('.upload-individual-essay').hide();
$('.upload-individual-essay:first').show();

Place of submit tag can be set via css styles 可以通过CSS样式设置提交标签的位置

And for my point of view it would be cleaner to understand. 以我的观点,它会更容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM