我想自定义确认对话框。 在我定制确认对话框之前,一切都很完美当我点击删除链接
<%= link_to image_tag("icon_delete.png", :border => 0), user, method: :delete ,data: { confirm: 'Are you sure delete:' + user.email } %>
浏览器显示默认确认弹出窗口,然后按下OK-&gt;此链接调用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)
这一次,当我点击删除链接 -
show()
的链接,而不是destroy()
- &GT;这里发生了什么,在我实现自定义确认对话之前,一切都运行良好。我没有改变route.rb中的任何内容。 我使用的是Ruby版本 1.9.3和Rails版本4.0.0.My asset / javascripts / application.js
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
的Gemfile
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'
有人可以帮助我吗?
答案 0 :(得分:0)
看起来您正在使用此处描述的实现: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
答案 1 :(得分:0)
花了两天时间寻找解决方案。在文件application.js中,只需添加 行:
//= require bootstrap-modal
按顺序bootstrap modal()working.Everything now now