I'm using Rails 6, got this error again.
show.html.erb
<%= link_to "Delete", post_path(@post), class: "dropdown-item", method: :delete %>
controller and routes are set up without error.
app/javascript/packs/application.js
require("@rails/ujs").start();
// require("turbolinks").start();
require("@rails/activestorage").start();
require("channels");
When click on the link, I can see the log:
Started GET "/posts/19" for ::1 at 2019-09-04 23:50:30 +0900
Processing by PostsController#show as HTML
Parameters: {"id"=>"19"}
Instead of DELETE.
This is what got rendered in HTML:
<a class="dropdown-item" rel="nofollow" data-method="delete" href="/posts/19">Delete</a>
In layouts I include application.js:
<%= javascript_pack_tag 'application' %>
In 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>
Actually change to button_to works, it generates this on 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>
Notice this line: <input type="hidden" name="_method" value="delete">
I think the problem is Rails UJS hasn't been loaded. I open console and type Rails or Rails.handleMethod and I got
Uncaught ReferenceError: Rails is not defined
at <anonymous>:1:1
(anonymous) @ VM9848:1
I was able to solve the problem. I will share for you guys.
In my layout I have
=javascript_pack_tag 'application'which load Rails UJS already.In my view partial I also include another js script
javascript_pack_tag 'posts/show'. The problems is in that script, I set to listen to click event thene.preventDefault(), turn out that Rails UJS can't create form delete, then GET request is sent.Rails UJS