代码之家  ›  专栏  ›  技术社区  ›  Oliver Trampleasure

Ruby远程部分被渲染两次

  •  0
  • Oliver Trampleasure  · 技术社区  · 7 年前

    今天下午我一直在四处移动代码,尝试不同的事情,等等,但是我无法解决我的(可能很简单)问题。我的索引布局上有一个按钮,它在模式对话框中显示一个部分窗体,用于a)创建新条目或b)编辑现有条目。我正在使用物化的、简单的表单和客户端验证gems。

    当我点击一个新条目的按钮时,模式会出现两次,看起来有点垃圾,但更重要的是,当我试图保存一个条目时,会发出两个POST请求。

    很明显,我只想让窗体部分呈现一次…但我不明白为什么会发生两次。

    谢谢你的帮助,如果你需要的比我下面提供的更多,请告诉我。


    这是中的按钮代码 索引文件 布局

    <div class="fixed-action-btn">
    
        <%= link_to "<i class='material-icons'>add</i>".html_safe, 
        new_patient_path, :remote => true, :id => 'new_patient_link', 
        :class => "btn-floating btn-large red" %>
    
    </div>
    

    然后加载javascript J.E.B

    $('#modal_partial').html('<%= escape_javascript(render('form')) %>');
    

    从而使 H.ML.Erb 部分到以下分区(存储在 共享/\u footer.html.erb 部分并位于索引页的底部。

    <div id="form_modal_wrap">
      <div id="modal_partial" class="modal modal-fixed-footer">
      </div>
    </div>
    

    以及 H.ML.Erb 部分:

    <div id="edit_patient_modal" class="";>
    <%= simple_form_for @patient, validate: true, remote: true do |f| %>
    
      <div class="modal-content" style="padding: 0px;">
    
        <h4 class="col s12" style="margin: 0px; padding: 10px 25px;">New Patient</h4>
    
    
        <%= f.error_notification %>
    
    
        <div class="row">
    
          <div class="col s12" style="padding: 0px;">
    
            <ul class="tabs" style="padding: 0px;">
    
              <li class="tab col"><a class="active"  href="#demographics">Demographics</a></li>
              <li class="tab col"><a class="active"  href="#admission">Admission</a></li>
              <li class="tab col"><a href="#presentation">Presentation</a></li>
              <li class="tab col"><a href="#jobs">Jobs</a></li>
              <li class="tab col"><a href="#results">Results</a></li>
    
            </ul>
    
          </div>
    
    
          <div id="demographics" class="col s12" style="padding: 20px;">
    
            <%= f.input :nhs, :label => "NHS"  %>
    
            <%= f.input :fname, :label => "Firstname" %>
            <%= f.input :lname, :label => "Lastname", validate: { presence: true, uniqueness: false }  %>
            <%= f.input :dob, :label => "DOB", as: :date, start_year: Date.today.year - 110,
                              end_year: Date.today.year - 12,
                              order: [:day, :month, :year] %>
    
            <%= f.input :mrn, :label => "MRN" %>
    
    
    
          </div>
    
          <div id="admission" class="col s12" style="padding:20px;" >
    
            <%= f.association :location %>
            <%= f.input :bed, :label => "Bed" %>
            <%= f.association :team, input_html: { multiple: true } %>
    
          </div>
    
          <div id="presentation" class="col s12" style="padding:20px;">
    
    
    
            <%= f.input :dx, :label => "Dx" %>
            <%= f.input :pmh, :label => "PMH" %>
    
    
          </div>
    
          <div id="jobs" class="col s12" style="padding:20px">
    
            <p>This job list is shared between all teams.</p>
            <%= f.input :jobs, :label => "Jobs" %>
    
          </div>
    
          <div id="results" class="col s12" style="padding:20px;">
    
    
          <table>
            <tr>
              <th>Group</th>
              <th>Result</th>
              <th>Date</th>
            </tr>
            <% @patient.results.each do |result| %>
              <tr>
                <td>
                  <%= Investigation.find(result.investigation_id).group %>
                </td>
                <td>
                  <%= Investigation.find(result.investigation_id).short %>
                  <%= result.value %>
                </td>
                <td>
                  <%= result.date %>
                </td>
              </tr>
            <% end %>
          </table>
          </div>
    
        </div>
    
    
    
      </div>
    
    
      <div class="modal-footer"> 
    
        <a id="modal_close" href="#!" class="modal-close waves-effect waves-pink btn-flat">Cancel</a>
        <%= f.button :submit, :class => "waves-effect btn" %>
    
      </div>
    
    
    
    <% end -%>
    

    下面的javascript位于 H.ML.Erb 部分:

      $(document).ready(function(){
        $('#modal_partial .tabs').tabs();
        $('#modal_partial select').formSelect();
        $('#modal_partial').modal();
        $('#modal_partial').modal('open');
        $('form').enableClientSideValidations(); 
      });
    
      $("#modal_close").on("click", function(e) {
        e.preventDefault();
        $('#modal_partial').modal('close');
      });
    
    </script>
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   Oliver Trampleasure    7 年前

    所以我终于明白了我到底做了什么…

    在里面 应用程序 我添加了以下两项:

    //= require jquery_ujs
    //= require rails-ujs
    

    通过删除其中任何一个模态,只出现一次。在阅读文档之后,我只需要Rails UJS,因为它执行的功能与jQuery_JS相同,但不依赖于jQuery。这并不意味着您仍然不能将jquery作为一个独立的需求(我仍然这样做)。

    所以解决办法是 删除行//=需要jquery ujs 只需要以下几点:

    //= require rails-ujs
    

    谢谢你的帮助。

        2
  •  1
  •   lacostenycoder    7 年前

    调试的唯一方法是检查代码中加载JavaScript(包括布局)的每个部分。某些原因导致表单重复,但如果没有看到所有代码,就无法进行调试。

    推荐文章