我的编辑/更新模式无法正常工作。我希望它更新数据库,关闭模式并将用户返回到我的索引页面,其中包含我的产品的表应该被更新。现在它只是更新数据库,但不关闭模式,用户必须刷新浏览器才能看到更新的记录。我正在使用这些 gem
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 部分,其中创建了编辑链接:
<% @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 %>
这是我的产品 Controller 的编辑和更新操作
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")
这是我的_编辑部分
<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
.
更新:
在更新
Controller 操作结束时进行重定向:
def update
@products = Product.all
@product = Product.find(params[:id])
@product.update_attributes(product_params)
redirect_to your_index_path
end
关于jquery - Bootstrap 模式未关闭并刷新索引页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31774084/