gpt4 book ai didi

ruby-on-rails - Rails 6 - 尽管添加了 rails/ujs,但破坏了 GET 路径

转载 作者:行者123 更新时间:2023-12-04 15:41:01 25 4
gpt4 key购买 nike

我正在使用 Rails 6,再次遇到此错误。

显示.html.erb

<%= link_to "Delete", post_path(@post), class: "dropdown-item", method: :delete %>

Controller 和路由设置没有错误。

应用程序/javascript/packs/application.js

require("@rails/ujs").start();
// require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");

当点击链接时,我可以看到日志:

Started GET "/posts/19" for ::1 at 2019-09-04 23:50:30 +0900
Processing by PostsController#show as HTML
Parameters: {"id"=>"19"}

而不是删除。

这是在 HTML 中呈现的内容:

<a class="dropdown-item" rel="nofollow" data-method="delete" href="/posts/19">Delete</a>

在布局中我包含了 application.js:

<%= javascript_pack_tag 'application' %>

在 HTML 中:

<head>
<meta name="csrf-param" content="authenticity_token">
<meta name="csrf-token" content="+EskpcJWxzYlo5g5lOyRAnx7tgARKbeFqV21YgtRjnlT4/xsmWsRljfqbVmpSDSl2l0s9scz7+C2bezpiLy4fA==">


<link rel="stylesheet" media="all" href="/assets/trix/dist/trix.self-6dd442511a4fc56685fca816ea1b42de7bbc8a0fd0bfd759530fa9e7625d1a6f.css?body=1" data-turbolinks-track="reload">
<link rel="stylesheet" media="all" href="/assets/actiontext.self-a679dceacb4aa0b812495eb34e20291bc8eab82506add17d18f0b3b3d1353123.css?body=1" data-turbolinks-track="reload">
<link rel="stylesheet" media="all" href="/assets/application.self-2115ce8ad5c8bf2c98589b69e9fc7c0d3ba5251b0d5ef251ec5b3da1da9cb7a8.css?body=1" data-turbolinks-track="reload">
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/toastify-js/src/toastify.min.css">
<script src="/packs/js/application-dffd55b262c09e354e1b.js"></script>
</head>

实际改为button_to有效,它在 HTML 上生成:

<form class="button_to" method="post" action="/posts/19">
<input type="hidden" name="_method" value="delete">
<input class="dropdown-item" type="submit" value="Delete">
<input type="hidden" name="authenticity_token" value="ZnfQ05Ah/4LevuR8vXvcYN/CSNAoTDQuwpGHF4WrJDiVV8+wwb4ztsYdsKk9UPZ3k9Gxpfpd/gcobcSBEI6/6A==">
</form>

注意这一行:<input type="hidden" name="_method" value="delete">

我认为问题在于 Rails UJS 尚未加载。我打开控制台并输入 RailsRails.handleMethod我得到了

Uncaught ReferenceError: Rails is not defined
at <anonymous>:1:1
(anonymous) @ VM9848:1

最佳答案

我能够解决问题。我会分享给你们。

在我的布局中,我有 =javascript_pack_tag 'application' 已经加载了 Rails UJS。

在我看来部分我还包括另一个 js 脚本 javascript_pack_tag 'posts/show'。问题出在那个脚本中,我设置为监听点击事件然后 e.preventDefault(),结果是 Rails UJS 无法创建表单删除,然后发送 GET 请求。

document.addEventListener("DOMContentLoaded",function() {
let postOption = document.getElementsById("post-options");
postOption.addEventListener("click", function(e) {
e.stopPropagation();
this.classList.toggle("is-active");
});
});
});

Rails UJS

    // Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
handleMethod: function(link) {
var href = rails.href(link),
method = link.data('method'),
target = link.attr('target'),
csrfToken = rails.csrfToken(),
csrfParam = rails.csrfParam(),
form = $('<form method="post" action="' + href + '"></form>'),
metadataInput = '<input name="_method" value="' + method + '" type="hidden" />';

if (csrfParam !== undefined && csrfToken !== undefined && !rails.isCrossDomain(href)) {
metadataInput += '<input name="' + csrfParam + '" value="' + csrfToken + '" type="hidden" />';
}

if (target) { form.attr('target', target); }

form.hide().append(metadataInput).appendTo('body');
form.submit();
},

关于ruby-on-rails - Rails 6 - 尽管添加了 rails/ujs,但破坏了 GET 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57791063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com