ruby-on-rails - 如何用 bootstrap 的模态显示 'Delete Confirm Dialog' ?不像浏览器的默认设置

标签 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 的模式显示“删除确认对话框”吗?

更新:

assets/javascripts/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

最佳答案

The blog entry使用 CoffeeScript 。假设您正在使用 :confirm Rails 表单中的选项,那么您需要覆盖 Rails 中的默认操作,方法是将以下代码放入 <controller>.js.coffee 中文件在您的 Assets 管道中 ( app/assets/javascript ):

$.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)

然后,您可以在 View 中使用这样的链接,它们应该显示 Bootstrap 模式而不是标准浏览器弹出窗口:

<%= link_to 'Delete', item, :method => :delete, :confirm=>'Are you sure?' %>

更新

这对我有用 :remote => true选项也是如此。

因此,如果我的 index.html.erb View 中有类似以下内容:

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

Controller 中的销毁操作有 format.js在response_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

这在 destroy.js.erb 中:

$("tr#<%= @post.id %>").fadeOut();

然后就一切正常了。当您单击 Destroy链接后,会弹出Bootstrap确认对话框,单击“确定”后,该行淡出并已在服务器上销毁。

关于ruby-on-rails - 如何用 bootstrap 的模态显示 'Delete Confirm Dialog' ?不像浏览器的默认设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14192009/

相关文章:

jquery - $ ("form").submit(function() { 在 Firefox 中不起作用

ruby-on-rails-3 - Rails 3 route 的连字符

javascript - Bootstrap 轮播 : how to know which slider it is when carousel is paused

ruby-on-rails - 日志 rails 控制台总是在生产环境中归档

html - 样式化了一个 HRef Float right 不工作 CSS

jquery - Bootstrap 标签输入 - 如何预填充对象值

ruby-on-rails - Ruby on Rails 的 Twitter Gem 不工作。我错过了什么?

ruby-on-rails - has_many 的表格 :through

ruby-on-rails - postgresql:创建角色不起作用

mysql - 带 mysql 和 omniauth 的 Rails 3 => [BUG] 段错误