I have two models: site
, and heading
, site has_many headings.
I have a destroy link:
<%= link_to site_heading_path(@site, @heading), data: { 'turbo-method': :delete, 'turbo-confirm': "Are you sure?"} do %>
<i ></i> Destroy
<% end %>
Here's my headings controller method, it's basically just the stock action:
def destroy
@heading.destroy
respond_to do |format|
format.html { redirect_to site_path(@site), notice: "Heading was successfully destroyed." }
format.json { head :no_content }
end
end
I've written this code a thousand times, but for some reason in my barebones new Rails 7 codebase it is doing something wonky.
Expected:
Heading is deleted, i'm redirected to /sites/1
Actual Behavior:
- Heading is deleted (good)
- I can see the redirect (good)
- TURBO_STREAMS for some reason calls DELETE on
/sites/1
(very bad)
I have no idea why this would happen.
Here is the webrick output. You can see it tried to delete the site too. I don't know why..
CodePudding user response:
Actually looks like this is a documented open issue on rails :)
https://github.com/hotwired/turbo-rails/issues/259
This comment on the above thread might help:
https://github.com/hotwired/turbo-rails/issues/259#issuecomment-1013852530
CodePudding user response:
This isn't a new issue, here it is from 2013:
Rails Redirect After Delete Using DELETE Instead of GET
but, it became very relevant with Turbo. When redirecting from a javascript DELETE request, browsers will use DELETE for the redirected location. The solution is to use 303
redirect which will always be a GET request:
redirect_to site_path(@site), status: 303
redirect_to site_path(@site), status: :see_other
I guess this would count as documented:
https://turbo.hotwired.dev/handbook/drive#redirecting-after-a-form-submission
After a stateful request from a form submission, Turbo Drive expects the server to return an HTTP 303 redirect response, which it will then follow and use to navigate and update the page without reloading.