如何使用甜蜜警报 2 执行 rails 命令? [英] How to execute a rails command with sweet alert 2?

查看:64
本文介绍了如何使用甜蜜警报 2 执行 rails 命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下 sweet alert 2 消息:

<button onclick='
    Swal.fire({
      title: "Select a page you want to be redirected:",
      showDenyButton: true,
      showCancelButton: true,
      confirmButtonText: "Home",
      denyButtonText: "About",
    }).then((result) => {
      /* Read more about isConfirmed, isDenied below */
      if (result.isConfirmed) {
        Swal.fire("Saved!", "", "success");
      } else if (result.isDenied) {
        Swal.fire("Changes are not saved!", "", "info");
      }
    });
'>SHOW SWEET ALERT</button>

该设计看起来工作正常,但问题是我无法执行 .erb 命令而不是显示另一个甜蜜的警报消息!

The design looks to work okay, but the issue is that I cannot execute an .erb command instead of showing another sweet alert message!

例如,如果我将上面的代码更改为以下代码,它会将一切搞砸,我不知道是什么原因造成的!:

So for example if I change the above code to the following it will mess everything up and I don't know what is causing this!:

<button onclick='
    Swal.fire({
      title: "Select a page you want to be redirected:",
      showDenyButton: true,
      showCancelButton: true,
      confirmButtonText: "Home",
      denyButtonText: "About",
    }).then((result) => {
      /* Read more about isConfirmed, isDenied below */
      if (result.isConfirmed) {
        <%= link_to "Home Page", root_path, class:"navbar-brand" %>
      } else if (result.isDenied) {
        <%= link_to "About Page", home_about_path, class:"nav-link" %>
      }
    });
'>SHOW SWEET ALERT</button>

代码片段 2 将给出以下错误:Uncaught SyntaxError: Unexpected token '<'

CODE SNIPPET 2 WILL GIVE THE FOLLOWING ERROR: Uncaught SyntaxError: Unexpected token '<'

第一个片段的图像示例:

Image example of first snippet:

第二个片段的图像示例:

Image example of second snippet:

推荐答案

实际上这是预期的行为.

Actually that is the expected behaviour.

正如您在第二个屏幕截图中看到的,ERB 解析器在您的 JavaScript 代码中生成了正确的锚标记,从而导致了 SyntaxError.(你不能在 JavaScript 中使用 HTML!除非它们在字符串引号周围.)

As you can see in the second screenshot, the ERB parser has generated the correct anchor tags inside your JavaScript code causing the SyntaxError. (You can't have HTML in JavaScript! Unless they are around string quote marks of course.)

你想做的是:

<button onclick='
    Swal.fire({
      title: "Select a page you want to be redirected:",
      showDenyButton: true,
      showCancelButton: true,
      confirmButtonText: "Home",
      denyButtonText: "About",
    }).then((result) => {
      /* Read more about isConfirmed, isDenied below */
      if (result.isConfirmed) {        
        location.pathname = "<%= root_path %>"
      } else if (result.isDenied) {
        location.pathname = "<%= home_about_path %>"
      }
    });
'>SHOW SWEET ALERT</button>

location.pathname = x"将引导您到相对路径.

"location.pathname = x" will direct you to a relative path.

服务器的 ERB 解析器将用您要查找的路径替换 root_path 和 home_about_path 变量.

The server's ERB parser will replace root_path and home_about_path variables with the paths you're looking for.

这篇关于如何使用甜蜜警报 2 执行 rails 命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆