`

Custom dialog for data-confirm in Rails

 
阅读更多

Every Rails developers might be already familiar with data-confirmattribute for a link. It is used to ask for user confirmation before executing the action on behalf of user. Here is one example

 

 

<%= link_to 'Destroy', product,
  :confirm => '删除后无法恢复,你确定删除吗?',
  :method => :delete %>

 

 

The generated link looks like this:

 

<a href="/products/4" data-confirm="删除后无法恢复,你确定删除吗?"
 data-method="delete" rel="nofollow">Destroy</a>

 

 

When users click on this link, they will see a confirmation dialog to ask them if they are sure to delete that post. Some developers will wonder how it is implemented on Rails, right? Actually, there is no magic in it. If you look at application.js file in your Rails app, you will mostly see the following line

 

 

//= require jquery_ujs

 

 

 

Some developers look at it too quickly and they thought that is jQuery UI library. NO, it is not. This is jquery-ujs (https://github.com/rails/jquery-ujs) and it allows Rails to handle links with custom data- attributes such as data-method or data-confirm. More specifically, it will handle the click events on any links have these attributes and prevent the default action if necessary. You can take a look at this file if you are interested in understanding the logic behind it, https://github.com/rails/jquery-ujs/blob/master/src/rails.js

One thing I don't like about data-confirm is that it uses Javascript confirmmethod, with the default confirmation dialog of the browsers. There are many fancy and beautiful dialogs out there for us to use, and I think it would be much better if we can integrate them into our application instead of using the default one. For example, take a look at two favorite libraries which I like to use: Bootbox, SweetAlert

In order to do this, we will need to override the event handler of jquery-ujs and handle the event on our own. I created a JS script for doing this (I actually took the idea from a guy on the Internet or Stackoverflow but I forgot the link to that post). You can just copy this script and put this in your application.js.

 

//Override the default confirm dialog by rails
$.rails.allowAction = function(link){
  if (link.data("confirm") == undefined){
    return true;
  }
  $.rails.showConfirmationDialog(link);
  return false;
}
//User click confirm button
$.rails.confirmed = function(link){
  link.data("confirm", null);
  link.trigger("click.rails");
}
//Display the confirmation dialog
$.rails.showConfirmationDialog = function(link){
  var message = link.data("confirm");
  $("#dialog-confirm").dialog({
    resizable: false,
    height: "auto",
    width: 400,
    modal: true,
    buttons: {
      "确定": function() {
        $(this).dialog("close");
        $.rails.confirmed(link);
      },
      "取消": function() {
        $(this).dialog("close");
      }
    }
  });
  $("#dialog-confirm-content").html(message);
  $("#dialog-confirm").dialog("open");
}

 

Explanation

We first override allowAction method with our own method. This is the method called by jquery-ujs when user clicks on a link with data-attributes. If the link does not contain "data-confirm" attribute, we will just return and let users execute the action as normal. Otherwise, we show the confirmation dialog.

 

//Override the default confirm dialog by rails
$.rails.allowAction = function(link){
  if (link.data("confirm") == undefined){
    return true;
  }
  $.rails.showConfirmationDialog(link);
  return false;
}

 Next, we handle the case in which user clicks on the Confirm button

//User click confirm button
$.rails.confirmed = function(link){
  link.data("confirm", null);
  link.trigger("click.rails");
}

 

Notice that we set data-confirm attribute to null to avoid recursive loop and then trigger click.rails event to let the default event handler to execute the action

Finally, we handle the function to show the dialog to users.

 

//Display the confirmation dialog
$.rails.showConfirmationDialog = function(link){
  var message = link.data("confirm");
  $("#dialog-confirm").dialog({
    resizable: false,
    height: "auto",
    width: 400,
    modal: true,
    buttons: {
      "确定": function() {
        $(this).dialog("close");
        $.rails.confirmed(link);
      },
      "取消": function() {
        $(this).dialog("close");
      }
    }
  });
  $("#dialog-confirm-content").html(message);
  $("#dialog-confirm").dialog("open");
}

 

In the above script, I use JqueryUI modal-confirmation  plugin . If you want to use another library, just replace the code starting with$("#dialog-confirm")with your own modal. You would need to pay attention to the logic of handling Confirm and Cancel button to make it work properly although it is pretty straightforward.

 

/project/app/views/layouts/application.html.erb

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Depot</title>
  <%= stylesheet_link_tag "application","depot","scaffold","jquery-ui-1.12.1-css/jquery-ui", :media => "all" %>
  <%= javascript_include_tag "application","jquery","jquery-ui-1.12.1-js/jquery-ui" %>
  <%= csrf_meta_tags %>
</head>
<body id="store">
  <div id="banner">
    <%= image_tag( "logo.png" ) %>
    <%= @page_title || "Pragmatic Bookshelf" %>
  </div>
  <div id="columns">
    <div id="side">
    </div>
    <div id="main">
      <%= yield %>
    </div>
  </div>
</body>
<div id="dialog-confirm" title="确认对话框" style="display:none;">
  <div id="confirm-imgs"><img src="/assets/tips.png" ></div>
  <div id="dialog-confirm-content">These items will be permanently deleted and cannot be recovered. Are you sure?</div>
</div>
</html>

 

css

 

#dialog-confirm #confirm-imgs{
    display: inline-block;
    vertical-align: middle;
}

#dialog-confirm #confirm-imgs img{
    width:60px;
    height:60px;
}

#dialog-confirm #dialog-confirm-content{
    display: inline-block;
}
#dialog-confirm-content {
    font-size:16px;
}

 



 

That's it. Let's forget boring confirm window and start using your beautiful modal.

 

http://stackoverflow.com/questions/7435859/custom-rails-confirm-box-with-rails-confirm-override

http://lesseverything.com/blog/customizing-confirmation-dialog-in-rails/#

http://stackoverflow.com/questions/4421072/jquery-ui-dialog-instead-of-alert-for-rails-3-data-confirm-attribute

 

 

  • 大小: 11.8 KB
分享到:
评论

相关推荐

    jquery-confirm设置按钮显示中文

    而jquery-confirm则是一个基于jQuery的插件,用于创建美观的弹窗提示,包括确认对话框、警告对话框和自定义对话框。这个插件在网页应用中非常常见,特别是在需要用户确认操作或展示重要信息时。 这篇博客文章...

    安卓Dialog对话框相关-项目通用Dialog.rar

    在Android开发中,Dialog对话框是一种非常常见的用户交互元素,用于在主界面之上显示临时信息或者请求用户输入。本资源“安卓Dialog对话框相关-项目通用Dialog.rar”包含了一些Dialog的实现示例,可能用于帮助开发者...

    dialog-1.2-5.20130523.el7.x86_64.rpm

    dialog-1.2-5.20130523.el7.x86_64.rpm,用于CentOS 7.7.1908 系统或者RedHat 7.X 系统 for x86_64使用

    dialog-1.1-9.20080819.1.el6.i686.rpm

    dialog-1.1-9.20080819.1.el6.i686.rpm

    安卓Dialog对话框相关-Custom-Dialog.zip

    Custom-Dialog-master这个目录可能包含了自定义Dialog的相关代码。通常,自定义Dialog涉及到以下几个步骤: 1. 创建一个新的布局文件:在res/layout目录下,为Dialog创建一个XML布局文件,定义Dialog的内容和外观。...

    dialog-1.1-10.20080819.el6.x86_64.rpm

    dialog-1.1-10.20080819.el6.x86_64.rpm,用于CentOS 6.10 系统或者RedHat 6.X 系统 for x86_64使用

    ra-delete-with-custom-confirm-button:用于带有React-admin的自定义确认对话框的Delete按钮

    安装# via npmnpm install --save ra-delete-with-custom-confirm-button# via yarnyarn add ra-delete-with-custom-confirm-button演示版克隆此存储库后,运行以下命令: cd example/yarn installyarn start 然后...

    Dialog 测试-----------

    在Android开发中,`Dialog` 是一个非常重要的组件,它用于在用户界面中显示临时的、半透明的窗口,通常用来提示用户进行某些操作或显示关键信息。标题“Dialog 测试-----------”暗示我们将讨论关于 `Dialog` 的各种...

    android dialog------普通对话框

    在Android开发中,Dialog是一种非常常见的用户界面组件,它用于在主界面之上显示一个临时的、半透明的小窗口,用于提示用户或者获取用户的输入。在本文中,我们将深入探讨如何在Android应用中创建和使用普通的Dialog...

    jQuery对话框Dialog弹出层插件演示与使用说明

    jQuery Dialog 是一个强大的弹出层插件,常用于创建各种对话框、提示窗口或模态框,极大地丰富了网页的交互体验。这个插件是基于 jQuery 库构建的,因此在使用前确保已经引入了 jQuery。下面将详细介绍如何使用 ...

    Python库 | dialog-api-1.32.0.tar.gz

    《Python库——dialog-api-1.32.0深度解析》 在编程世界里,Python以其简洁明了的语法和强大的库支持,成为了广大开发者钟爱的开发语言。本文将聚焦于一个特定的Python库——dialog-api-1.32.0,探讨其功能、用途及...

    ra-custom-confirm:您的React-admin的自定义确认对话框

    ra-custom-confirm 您的自定义确认对话框。 安装 # via npm npm install --save ra-custom-confirm # via yarn yarn add ra-custom-confirm 演示版 克隆此存储库后,运行以下命令: cd example/ yarn install yarn...

    PyPI 官网下载 | dialog_api-1.33.1-py3-none-any.whl

    **PyPI官网下载 | dialog_api-1.33.1-py3-none-any.whl** 在Python编程领域,PyPI(Python Package Index)是官方的软件仓库,它为开发者提供了发布和分享Python库的平台。资源"dialog_api-1.33.1-py3-none-any.whl...

    PyPI 官网下载 | dialog-api-1.73.0.tar.gz

    **PyPI 官网下载 | dialog-api-1.73.0.tar.gz** PyPI(Python Package Index)是Python开发者最常使用的软件包仓库,它提供了丰富的Python库和模块供全球用户下载和使用。在本案例中,我们关注的是一个名为`dialog-...

    安卓Dialog对话框相关-HerilyAlertDialog完全自定义的Dialog.zip

    【Android Dialog对话框相关-HerilyAlertDialog完全自定义的Dialog】 在Android开发中,Dialog对话框是一种常见的用户交互元素,用于向用户展示重要的信息或获取用户的输入。Android系统提供了多种预定义的Dialog...

    Python库 | dialog_api-1.66.0-py3-none-any.whl

    资源分类:Python库 所属语言:Python 资源全名:dialog_api-1.66.0-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059

    安卓Dialog对话框相关-androiddialog总结Dialog整理.rar

    本资源“安卓Dialog对话框相关-androiddialog总结Dialog整理.rar”提供了关于Android Dialog的总结和示例,虽然不能确保所有代码都能直接运行,但它可以作为学习和参考的宝贵资料。 首先,我们来看Dialog的基本用法...

    安卓Dialog对话框相关-dialogplus.zip

    【Android Dialog 对话框详解】 在 Android 应用开发中,`Dialog` 是一个非常重要的组件,它用于向用户展示一些重要信息或者进行交互操作。`Dialog` 不是全屏显示,而是浮现在当前活动(Activity)之上,通常用于...

    Bootstrap中data-target 到底是什么

    Bootstrap中的`data-target`属性是该框架中一个关键的JavaScript交互元素,主要用于引导插件(如模态框、下拉菜单、工具提示等)的行为。它与`data-toggle`属性一起工作,帮助开发者无需编写复杂的JavaScript代码就...

Global site tag (gtag.js) - Google Analytics