Bootstrap 模态不关闭并刷新索引页

Bootstrap modal not closing and refreshing index page

我的 edit/update 模式无法正常工作。我希望它更新数据库,关闭模式并 return 用户返回到我的索引页面,其中包含我的产品的 table 应该被更新。现在它只是更新数据库,但没有关闭模式,用户必须刷新浏览器才能看到更新的记录。我正在使用这些 gems

gem 'rails', '4.2.1'
gem 'bootstrap-sass', '~> 3.3.5'
gem 'responders', '~> 2.0'

这是我的 index.html.erb

...
 <tbody class="product-index">
   <%= render "index" %>
 </tbody>
</table> #starting <table> tag is up higher..
</div>
<div id="product-modal" class="modal fade">
<div class="modal-dialog">
<div id="inner-product-modal" class="modal-content"></div>
</div>
</div>

这是我的 _index 部分,其中创建了编辑 link:

 <% @products.each do |product| %>
<tr>
 <td><%= product.name %></td>
 <td><%= number_to_currency product.price %></td>
 <td><%= link_to "Edit", edit_product_path(product), remote: true, class: 
 "btn btn-default" %></td>
 <td><%= link_to "Delete", product_delete_path(product), remote: true, 
 class: "btn btn-danger" %></td>
</tr>
<% end %>

这是我的产品控制器的编辑和更新操作

 def edit
     @product = Product.find(params[:id])
 end

 def update
    @products = Product.all
    @product = Product.find(params[:id])
    @product.update_attributes(product_params)
 end

这是我的 edit.js.erb 文件

$("#inner-product-modal").html("<%= escape_javascript(render 'edit') %>");
$("#product-modal").modal("show")

这是我的 _edit 部分

<div class="modal-header">
  <h3><%= "Editing #{@product.name}" %></h3>
</div>
<%= render "form" %>

最后这是我的部分表格(为简洁起见进行了编辑)

<%= form_for @product, remote: true, html: { class: "form-horizontal", 
style: "display:inline;" } do |f| %>
    <div class="modal-body">
      <ul class="errors"></ul>
....

<div class="modal-footer">
    <%= f.submit class: "btn btn-primary" %>
    <%= link_to "Cancel", "#", class: "btn", data: {dismiss: "modal"} %>
</div>
<% end %>

如果您想使用正常的 post 方法提交更新请求并刷新页面,您应该从 form_for 助手中删除 remote: true

更新:

在您的 update 控制器操作结束时进行重定向:

def update
  @products = Product.all
  @product = Product.find(params[:id])
  @product.update_attributes(product_params)
  redirect_to your_index_path
end