[英]How can I dynamically create a delete button for a rails object using Javascript?
I have a script to dynamically show an index of items on a page when a "show" button is pressed but when I try to add a new button to delete each object shown it throws an error I cant seem to resolve.我有一个脚本可以在按下“显示”按钮时动态显示页面上的项目索引,但是当我尝试添加一个新按钮以删除显示的每个对象时,它会抛出一个我似乎无法解决的错误。 Here is the whole script:
这是整个脚本:
<script type="text/javascript" charset="utf-8">
$(function() {
$(".js-view-decks").on("click", function() {
var id = $(this).data("id");
$.get("/users/" + id + "/decks.json", function(data) {
var complete = ""
data.forEach(function(deck) {
const markup = `
<fieldset>
<legend><h4> ${deck.name}
<%= button_to "View Details", "/users/${deck.user_id}/decks/${deck.id}/", method: "get", form: { style: "display:inline-block"} %>
<%= button_to "Edit", "/users/${deck.user_id}/decks/${deck.id}/edit", method: "get", form: { style: "display:inline-block"} %>
<%= button_to "Delete", "/users/${deck.user_id}/decks/${deck.id}/", type: "POST", data: {"_method":"delete"}, form: { style: "display:inline-block"} %>
</h4></legend>
<p>
<%= "Format: ${deck.format}" %> <br/>
<%= "Cards: ${deck.deck_cards.length}" %>
</p>
</fieldset>
`
complete += markup
});
$("#user_decks").html(complete)
});
});
});
</script>
The error I am getting is this:我得到的错误是这样的:
bad URI(is not URI?): /users/${deck.user_id}/decks/${deck.id}/
The dynamically created "view details" and "edit" buttons work fine, but the delete button seems to be having a problem with the URL for some reason and I can't figure out why, any suggestions?动态创建的“查看详细信息”和“编辑”按钮工作正常,但由于某种原因,删除按钮的 URL 似乎有问题,我不知道为什么,有什么建议吗?
Edit: There seems to have been an issue trying to use the rails helper to generate the delete button.编辑:尝试使用 rails helper 生成删除按钮似乎存在问题。 Manually generating the button with a form seems to have worked as follows:
手动生成带有表单的按钮似乎工作如下:
<form method='post' action='/users/${deck.user_id}/decks/${deck.id}' data-remote='true' form={ style="display:inline-block"}>
<input name='_method' value='delete' type='hidden' />
<input value='Delete' type='submit' />
</form>
Yes, changing it to post breaks it the same way the third button was breaking
是的,将其更改为 post 会破坏它,就像第三个按钮被破坏一样
Ah, we're onto something.啊,我们正在做一些事情。 The problem here, then, is that button_to happens on server-side.
那么,这里的问题是 button_to 发生在服务器端。 Loooong before you attempt to interpolate a deck into those paths.
在您尝试将套牌插入到这些路径中之前,请大声朗读。 And
"/users/${deck.user_id}/decks/${deck.id}/"
, as it is, is not a valid uri.而
"/users/${deck.user_id}/decks/${deck.id}/"
,实际上,不是一个有效的 uri。 But button_to needs it to be a valid uri when you make a POST button.但是 button_to 在您制作 POST 按钮时需要它是一个有效的 uri。 (don't know why, but it's irrelevant. It is actually a surprise that it didn't fail in the other two cases)
(不知道为什么,但这无关紧要。在其他两种情况下没有失败实际上令人惊讶)
A possible solution would be to not use button_to
and write out the links/forms directly, without rails helpers.一个可能的解决方案是不使用
button_to
并直接写出链接/表单,而无需 Rails 助手。 That is, <form>
, not <%= form_for %>
and <a>
instead of <%= link_to %>
.也就是说,
<form>
,而不是<%= form_for %>
和<a>
而不是<%= link_to %>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.