jquery - Bootstrap 模式未关闭并刷新索引页面

标签 jquery ruby-on-rails twitter-bootstrap modal-dialog

我的编辑/更新模式无法正常工作。我希望它更新数据库,关闭模式并将用户返回到我的索引页面,其中包含我的产品的表应该被更新。现在它只是更新数据库,但不关闭模式,用户必须刷新浏览器才能看到更新的记录。我正在使用这些 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/

相关文章:

javascript - jquery中的Ajax调用刷新Django中的数据库模型

javascript - jquery 将 post 值发送到另一个页面并在当前页面加载。显示更多,显示更多按钮

jQuery 插入的 ActiveX <object/> 标记具有不同的行为

ruby-on-rails - 我在哪里确认使用 FactoryGirl 创建的用户?

javascript - 在 Rails 3 中使用 AJAX 删除记录

javascript - 谷歌 API 翻译器。如何只翻译一个 DIV?

ruby-on-rails - Rails 中用户注销后如何使所有 session 失效?

javascript - 如何使容器居中并使网页适合移动设备?

css - 为什么 Bootstrap 3 切换到 box-sizing : border-box?

twitter-bootstrap - 推特 Bootstrap 折叠菜单不起作用