3

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 class="ti ti-trash"></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:

  1. Heading is deleted (good)
  2. I can see the redirect (good)
  3. 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..

enter image description here

Matthew Rathbone
  • 8,144
  • 7
  • 49
  • 79
  • strange indeed. Almost like with the redirect to site_path, turbo-method: delete is being resubmitted? I am still stuck on rails 6 :) but anyway you can verify the turbo method on the redirect from the network calls? – Shaunak Jul 30 '22 at 04:22

2 Answers2

1

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.

Alex
  • 16,409
  • 6
  • 40
  • 56
0

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

Shaunak
  • 17,377
  • 5
  • 53
  • 84