Menu

1 / 2

My AI assistant kept fixing Rails delete buttons with a JSON API. Here's the rule that stopped it.

DEV Community: rails·bzucker4·2 days ago
#fFkV0dTO
#devtorails#devto#dev#delete#rails
Long article (763 words) — no AI summary yet.
Reading 0:00
15s threshold

Ask an AI coding assistant to fix a broken delete button in a Rails 8 app and there is a good chance you get something like this back:

skip_forgery_protection only : :destroy

def destroy @post . destroy render json : { ok : true } end

Enter fullscreen mode

Exit fullscreen mode

Plus a new /api route and a Stimulus controller that calls fetch() with method: "DELETE" .

It works. It is also the wrong fix. You just turned off CSRF protection on a destructive action and bolted a second architecture onto an app that was built to render HTML.

What actually broke

In older Rails apps, link_to "Delete", post, method: :delete relied on rails-ujs to turn a link into a DELETE request. Rails 7+ apps run on Turbo instead, and that old method: :delete on a link does nothing useful anymore. The click becomes a GET to the show page.

The assistant sees "delete doesn't work," notices Turbo is involved, and reaches for the pattern it has seen a million times in React tutorials: a JSON endpoint and a fetch call.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.