Hi Everyone
I will appreciate if anyone please help me with this issue,
My controller action is:
def destroy
@crypto.destroy
respond_to do |format|
format.html { redirect_to cryptos_url, notice: ‘it was successfully destroyed.’ }
format.json { head :no_content }
end
end
the index.html.erb is:
<%= link_to ‘Delete’, crypto_path(@crypto), data: {turbo_method: :delete} %>
** button_to works though.*
The reason why it is not working is because with data-turbo-method
attribute it will make a fetch request and will expect a turbo-response and not navigate. If you add data-turbo-frame="_top"
it will tell it to perform the navigation on the page and will redirect you correctly.
<%= link_to ‘Delete’, crypto_path(@crypto), data: {turbo_method: :delete, turbo_frame: "_top" } %>
What does the rails logs say when u make the request
You need to add a status: :see_other
inside the redirect_to method this is because turbo needs that to perform the navigation. Here is the docs
Redirect in controller would look like this
format.html { redirect_to cryptos_url, notice: "...", status: :see_other }
To correct my earlier answer I thought that data-turbo-method
will do a request without navigating but that is false. It will still navigate the page correctly so that means all you need is the data-turbo-method
method on the link and then the new status: :see_other
in the controller response
That change is dangerous, because often browsers will pre-fetch links to speed up the page transitions. You do NOT want to pre-fetch a logout link. That’s why they are set to use DELETE as the verb. Rails translates them into a POST with a _method
key of delete, but pre-fetchers don’t submit forms, and that keeps you safe.
Walter