ruby-on-rails – 如何使用bootstrap的模式显示“删除确认对话框”?不喜欢浏览器的默认值

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何使用bootstrap的模式显示“删除确认对话框”?不喜欢浏览器的默认值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在看这个页面( http://lesseverything.com/blog/archives/2012/07/18/customizing-confirmation-dialog-in-rails/)它显示了一些代码,但我不明白我如何将它实现到我的应用程序.

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

更新:

资产/ Java脚本/ application.js中

  1. // This is a manifest file that'll be compiled into application.js,which will include all the files
  2. // listed below.
  3. //
  4. // 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.
  5. //
  6. // It's not advisable to add code directly here,but if you do,it'll appear at the bottom of the
  7. // the compiled file.
  8. //
  9. // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED,ANY BLANK LINE SHOULD
  10. // GO AFTER THE REQUIRES BELOW.
  11. //
  12. //= require jquery
  13. //= require jquery-ui
  14. //= require twitter/bootstrap
  15. //= require_tree .
  16. //= require jquery.ui.datepicker
  17. //= require autocomplete-rails

解决方法

The blog entry使用coffeescript.假设您在rails表单中使用:confirm选项,那么您需要覆盖Rails中的默认操作,方法是将以下代码放在资产管道中的< controller> .js.coffee文件中(app / assets / javascript ):
  1. $.rails.allowAction = (link) ->
  2. return true unless link.attr('data-confirm')
  3. $.rails.showConfirmDialog(link) # look bellow for implementations
  4. false # always stops the action since code runs asynchronously
  5.  
  6. $.rails.confirmed = (link) ->
  7. link.removeAttr('data-confirm')
  8. link.trigger('click.rails')
  9.  
  10. $.rails.showConfirmDialog = (link) ->
  11. message = link.attr 'data-confirm'
  12. html = """
  13. <div class="modal" id="confirmationDialog">
  14. <div class="modal-header">
  15. <a class="close" data-dismiss="modal">×</a>
  16. <h3>#{message}</h3>
  17. </div>
  18. <div class="modal-body">
  19. <p>Are you sure you want to delete?</p>
  20. </div>
  21. <div class="modal-footer">
  22. <a data-dismiss="modal" class="btn">Cancel</a>
  23. <a data-dismiss="modal" class="btn btn-primary confirm">OK</a>
  24. </div>
  25. </div>
  26. """
  27. $(html).modal()
  28. $('#confirmationDialog .confirm').on 'click',-> $.rails.confirmed(link)

然后,您可以在视图中使用此类链接,它们应显示Bootstrap模式而不是标准浏览器弹出窗口:

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

UPDATE

这适用于我使用:remote =>真正的选择.

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

  1. <table>
  2. <tr>
  3. <th>Name</th>
  4. <th>Title</th>
  5. <th>Content</th>
  6. <th></th>
  7. <th></th>
  8. <th></th>
  9. </tr>
  10.  
  11. <% @posts.each do |post| %>
  12. <tr id="<%= post.id %>">
  13. <td><%= post.name %></td>
  14. <td><%= post.title %></td>
  15. <td><%= post.content %></td>
  16. <td><%= link_to 'Show',post %></td>
  17. <td><%= link_to 'Edit',edit_post_path(post) %></td>
  18. <td><%= link_to 'Destroy',post,method: :delete,:remote => true,data: { confirm: 'Are you sure?' } %></td>
  19. </tr>
  20. <% end %>
  21. </table>

并且控制器中的destroy动作在respond_to中有format.js:

  1. # DELETE /posts/1
  2. # DELETE /posts/1.json
  3. def destroy
  4. @post = Post.find(params[:id])
  5. @post.destroy
  6.  
  7. respond_to do |format|
  8. format.js
  9. format.html { redirect_to posts_url }
  10. format.json { head :no_content }
  11. end
  12. end

这在destroy.js.erb中:

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

一切都行之有效.单击“销毁”链接时,将弹出“引导程序确认”对话框,单击“确定”后,该行将淡出并在服务器上销毁.

猜你在找的Ruby相关文章