2017-04-15 132 views
0

我正在关注Rails和AJAX的教程,以及如何提交表单并在提交表单后立即加载结果表,但我在填充项目时遇到了一些问题表格提交时。Rails 5 + Ajax表格不提交表单提交

现在,表单提交时,数据被保存并在页面刷新后出现在表格中,但我确实需要过渡动画和即时页面更新。

从我玩过类似的东西到现在已经有一段时间了,所以它可能只是一个我似乎无法跟踪的小错误。这里

我的索引控制器动作是我的位置指数:

<div class="container"> 

    <div class="row" id="container_locations"> 
    <table class="table"> 
     <thead> 
     <tr> 
      <th><center>Unit number</center></th> 
      <th><center>Street number</center></th> 
      <th><center>Street name</center></th> 
      <th><center>Quad</center></th> 
      <th><center>City</center></th> 
      <th><center>Province</center></th> 
      <th><center>Postal code</center></th> 
      <th><center>Tel number</center></th> 
      <th colspan="3"></th> 
     </tr> 
     </thead> 

     <tbody> 
     <% if @locations.present? %> 
     <div id="containerLocations"> 
      <%= render @locations %> 
     </div> 
     <% else %> 
     <td colspan="11"><center><h5>no locations added. please add your first location now.</h5></center></td> 
     <% end %> 
     </tbody> 
    </table> 

    </div> 

    <%= link_to 'add office location', '#createLocation', 'data-toggle' => 'modal', :class => 'btn btn-outline-success createLocationBtn' %> 
</div> 

<%= render partial: 'locations/locations_form' %> 

我的形式分:

<div class="modal fade" id="createLocation" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true"> 
    <div class="modal-dialog modal-lg" role="document"> 
    <div class="modal-content"> 
     <div class="modal-header"> 
     <h5 class="modal-title" id="exampleModalLongTitle">Modal title</h5> 
     <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
      <span aria-hidden="true">&times;</span> 
     </button> 
     </div> 
     <div class="modal-body"> 
     <%= form_for(@location, remote: true) do |f| %> 

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

      <ul> 
      <% @location.errors.full_messages.each do |message| %> 
       <li><%= message %></li> 
      <% end %> 
      </ul> 
      </div> 
     <% end %> 
      <div class="row"> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :unit_number %> 
       <%= f.number_field :unit_number, :class => 'form-control unitNum' %> 
       </div> 
      </div> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :street_number %> 
       <%= f.number_field :street_number, :class => 'form-control streetNum' %> 
       </div> 
      </div> 
      </div> 

      <div class="row"> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :street_name %> 
       <%= f.text_field :street_name, :class => 'form-control streetName' %> 
       </div> 
      </div> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :quad, 'quadrant' %> 
       <%= f.text_field :quad, :class => 'form-control cityQuad' %> 
       </div> 
      </div> 
      </div> 

      <div class="row"> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :city %> 
       <%= f.text_field :city, :class => 'form-control cityName' %> 
       </div> 
      </div> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :province %> 
       <%= f.text_field :province, :class => 'form-control officeProv' %> 
       </div> 
      </div> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :postal_code %> 
       <%= f.text_field :postal_code, :class => 'form-control postalCode' %> 
       </div> 
      </div> 
      </div> 

      <div class="row"> 
      <div class="col"> 
       <div class="form-group"> 
       <%= f.label :tel_number %> 
       <%= f.text_field :tel_number, :class => 'form-control telNumber' %> 
       </div> 
      </div> 
      </div> 

     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> 
     <%= f.submit :class => 'btn btn-outline-success' %> 
     <% end %> 
     </div> 
    </div> 
    </div> 
</div> 

,这里是呈现到@locations部分:

<tbody> 
    <tr id="location_<%= location.id %>"> 
    <td><%= location.unit_number %></td> 
    <td><%= location.street_number %></td> 
    <td><%= location.street_name %></td> 
    <td><%= location.quad %></td> 
    <td><%= location.city %></td> 
    <td><%= location.province %></td> 
    <td><%= location.postal_code %></td> 
    <td><%= location.tel_number %></td> 
    <td><%= link_to 'Show', location %></td> 
    <td><%= link_to 'Edit', edit_location_path(location) %></td> 
    <td><%= link_to 'Destroy', location, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
</tbody> 

,最后这里是我的create.js.erb

$("#createLocation").modal('hide'); 

$(".unitNum").val(''); 
$(".streetNum").val(''); 
$(".streetName").val(''); 
$(".cityQuad").val(''); 
$(".cityName").val(''); 
$(".officeProv").val(''); 
$(".postalCode").val(''); 
$(".telNumber").val(''); 

$("#containerLocations").prepend('<%= j render @location %>'); 
$("#location_<%= @location.id %>").hide().fadeIn(3000); 

回答

0

因此,原来这是一个小问题,我无法追查,这个问题是我在我的create.js.erb引用

$("#containerLocations").prepend('<%= j render @location %>'); 

是需要:

$("#container_locations").prepend('<%= j render @location %>'); 

另一个问题是我将#container_locations放在索引文件中,我将它从表格上方的<div>中移出并放入<tr>块中,现在它的功能与它应该一样并将其放在适当的区域中。