【问题标题】:Rails-link delete isn't working property after customize confirmation delete dialog自定义确认删除对话框后,Rails-link delete 无效
【发布时间】:2013-10-10 02:56:22
【问题描述】:

我想自定义确认对话框。 在我自定义确认对话框之前,一切都很完美。当我点击删除链接时

<%= link_to image_tag("icon_delete.png", :border => 0), user, method: :delete ,data: { confirm: 'Are you sure delete:' + user.email } %>

浏览器显示默认确认弹出窗口,我按下 OK->此链接在 UserController 中调用 destroy()。 现在,我实现自定义确认弹出 在 user.js.coffee 中

$.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>Are you sure Mr. President?</h3>
       </div>
       <div class="modal-body">
         <p>#{message}</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)

这一次,当我点击删除链接时 -

  1. 浏览器未显示自定义确认弹出对话框或默认弹出对话框
  2. UserController 中的链接名为show() 而不是destroy()
    --> 这里发生了什么,在我实现自定义确认对话框之前一切正常。我没有更改 route.rb 中的任何内容。

我使用 Ruby 版本 1.9.3 和 Rails 版本 4.0.0。我的资产/javascripts/application.js

//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .

宝石文件

gem 'sass-rails', '~> 4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'

谁能帮帮我?

【问题讨论】:

    标签: ruby-on-rails twitter-bootstrap


    【解决方案1】:

    看起来您正在使用此处描述的实现:http://rors.org/demos/custom-confirm-in-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
    

    【讨论】:

    • 这与缩进问题无关。我在文件中的代码与您在上面发布的链接中的代码相同
    • 我在萤火虫控制台上看到,当我点击删除链接时,在它重定向到 UserController 上的 show() 之前,javascript 错误。但我无法捕捉到它?它发生得非常快
    • 您需要弄清楚如何解决该错误。这几乎肯定是罪魁祸首。
    • 我发现,firebug 控制台报错:$(html).modal is not a function. that line $(html).modal()-->cause error
    • 那意味着要么你没有包含bootstrap JS,要么它没有包含在你自己的代码之前。
    【解决方案2】:

    花了 2 天时间寻找解决方案。在 application.js 文件中,只需添加 行:

    //= require bootstrap-modal 
    

    为了引导 modal() 正常工作。现在一切都很完美

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多