【问题标题】:Bootstrap modal not closing and refreshing index page引导模式不关闭和刷新索引页面
【发布时间】:2015-08-02 16:36:28
【问题描述】:

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

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 %>

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

 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 %>

【问题讨论】:

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


    【解决方案1】:

    如果您想使用普通的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
    

    【讨论】:

    • Shoot.. 我试过了,但现在不是去索引页面而是去产品页面.. 我不知道为什么?
    • @ToddT 您必须在控制器操作结束时redirect 用户才能索引页面。查看更新答案。
    • 太棒了!!!不知道为什么我错过了那个..认为我很沮丧..但它有效!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2019-06-03
    • 2018-07-05
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    相关资源
    最近更新 更多