要将SweetAlert2 gem确认框重定向到链接,你可以使用SweetAlert2提供的then()方法来在用户点击确认按钮后执行重定向操作。
以下是一个示例代码,展示了如何使用SweetAlert2 gem将确认框重定向到链接:
# Gemfile
gem 'sweet-alert2-rails'
# application.js
//= require sweetalert2/dist/sweetalert2.min
# application.scss
@import 'sweetalert2/dist/sweetalert2.min.css';
# controller.rb
def redirect_to_link
respond_to do |format|
format.js
end
end
# redirect_to_link.js.erb
Swal.fire({
title: '确认重定向到链接吗?',
icon: 'warning',
showCancelButton: true,
confirmButtonText: '确定',
cancelButtonText: '取消'
}).then((result) => {
if (result.isConfirmed) {
window.location.href = 'https://example.com'; // 将链接替换为你要重定向到的实际链接
}
});
在上面的示例中,我们首先在Gemfile中添加了sweet-alert2-rails gem,并在application.js和application.scss文件中引入了SweetAlert2的CSS和JS文件。
然后,我们在控制器中定义了redirect_to_link方法,并在相应的视图文件redirect_to_link.js.erb中编写了SweetAlert2确认框的代码。当用户点击确认按钮时,我们使用JavaScript的window.location.href属性将页面重定向到指定的链接。
请注意,redirect_to_link.js.erb中的代码使用了ERB语法,因此它将在服务器端渲染,并在响应发送到客户端时被执行。
当用户访问redirect_to_link路径时,将显示SweetAlert2确认框,并在用户点击确认按钮后将页面重定向到指定的链接。
下一篇:按下确定键时智能面板重新启动。