如何使用bootstrap的模态显示"删除确认对话框"?不喜欢浏览器的默认值

cat*_*cat 6 ruby-on-rails ruby-on-rails-3 twitter-bootstrap

我正在看这个页面(http://lesseverything.com/blog/archives/2012/07/18/customizing-confirmation-dialog-in-rails/)它显示了一些代码,但我不明白我是怎么做的可以实现它到我的应用程序.

我使用Rails3.2,Bootstrap.css和JQuery.
有人能告诉我究竟需要做些什么来显示带有bootstrap模式的"删除确认对话框"吗?

更新:

资产/ Java脚本/ application.js中

// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
// GO AFTER THE REQUIRES BELOW.
//
//= require jquery
//= require jquery-ui
//= require twitter/bootstrap
//= require_tree .
//= require jquery.ui.datepicker
//= require autocomplete-rails
Run Code Online (Sandbox Code Playgroud)

mcc*_*nnf 13

博客条目使用coffeescript.假设您正在使用:confirmrails表单中的选项,那么您需要通过将以下代码放在<controller>.js.coffee资产管道(app/assets/javascript)中的文件中来覆盖Rails中的默认操作:

$.rails.allowAction = (link) ->
  return true unless link.attr('data-confirm')
  $.rails.showConfirmDialog(link) # look bellow for implementations
  false # always stops the action since code runs asynchronously

$.rails.confirmed = (link) ->
  link.removeAttr('data-confirm')
  link.trigger('click.rails')

$.rails.showConfirmDialog = (link) ->
  message = link.attr 'data-confirm'
  html = """
         <div class="modal" id="confirmationDialog">
           <div class="modal-header">
             <a class="close" data-dismiss="modal">×</a>
             <h3>#{message}</h3>
           </div>
           <div class="modal-body">
             <p>Are you sure you want to delete?</p>
           </div>
           <div class="modal-footer">
             <a data-dismiss="modal" class="btn">Cancel</a>
             <a data-dismiss="modal" class="btn btn-primary confirm">OK</a>
           </div>
         </div>
         """
  $(html).modal()
  $('#confirmationDialog .confirm').on 'click', -> $.rails.confirmed(link)
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用像这样的链接在你看来,他们应该显示引导模态,而不是标准的浏览器弹出:

<%= link_to 'Delete', item, :method => :delete, :confirm=>'Are you sure?' %>
Run Code Online (Sandbox Code Playgroud)

UPDATE

这也适用于我使用该:remote => true选项.

因此,如果我在index.html.erb视图中有类似以下内容:

<table>
  <tr>
    <th>Name</th>
    <th>Title</th>
    <th>Content</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @posts.each do |post| %>
  <tr id="<%= post.id %>">
    <td><%= post.name %></td>
    <td><%= post.title %></td>
    <td><%= post.content %></td>
    <td><%= link_to 'Show', post %></td>
    <td><%= link_to 'Edit', edit_post_path(post) %></td>
    <td><%= link_to 'Destroy', post, method: :delete, :remote => true, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>
Run Code Online (Sandbox Code Playgroud)

控制器中的destroy动作在format.jsrespond_to中有:

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post = Post.find(params[:id])
    @post.destroy

    respond_to do |format|
      format.js
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end
Run Code Online (Sandbox Code Playgroud)

这在destroy.js.erb中:

$("tr#<%= @post.id %>").fadeOut();
Run Code Online (Sandbox Code Playgroud)

一切都行之有效.单击该Destroy链接时,将弹出Bootstrap确认对话框,单击"确定"后,该行将淡出并在服务器上销毁.