rails: Rails 7: link_to delete not working (wrong redirect)

Steps to reproduce

View:

<%= link_to "Destroy", @question, data: { 'turbo-method': :delete, 'turbo-confirm': 'Are you sure?' } %>

Controller:

  def destroy
    @question = current_user.questions.find(params[:id])
    @question.destroy
    redirect_to questions_url, notice: "Question was successfully destroyed."
  end

Expected behavior

The question is deleted and the user redirected to the questions index.

Actual behavior

The question is deleted and the user is redirected to the question (instead of question index as defined in the controller!) where he gets an obvious error:

ActiveRecord::RecordNotFound in QuestionsController#show
Couldn't find Question with 'id'=6
inside def show

System configuration

Rails version: 7.0.1

Ruby version: 3.1

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22 (4 by maintainers)

Most upvoted comments

For anyone else coming across this, this solved the issue for me.

$ rails importmap:install
$ rails turbo:install stimulus:install

I saw those lines in the installation of a new Rails app with 7.0.2.3, where the associated files did not exist in my 7.0.2.2 installation. After running those commands and restarting my app,

<%= link_to "Delete", @event, class: "button",
              data: { turbo_method: :delete, turbo_confirm: "Really?!?" } %>

works perfectly.

HTH

try to add status to redirect like here “redirect_to root_path, status: :see_other” Look into examples https://guides.rubyonrails.org/getting_started.html

I know this is closed, but I’ve been fighting this whole delete tag helper since January. In the two apps I upgraded I ended up created a destroyConfirmTag helper that used stimulus to ask the confirm message.

I can confirm that both:

<%= link_to "Delete", @event, class: "button",
              data: { turbo_method: :delete, turbo_confirm: "Really?!?" } %>
and

<%= button_to "Delete", @event, class: "button", 
            form: {
                  data: { turbo_method: :delete, turbo_confirm: "Really?!?" } 
             } %>

work in the latest version of rails. I was just kind of lucky to find the button_to needed the form: key wrapping. But:

  1. Rails 7.0.3 scaffold now generates button_to links (without confirm tags, which are not documented)
  2. Getting started guide does not talk about button_to
  3. The redirect 303 stuff is not well known but it can get you ( I had all records in one model deleted!!!)
  4. I sense a move to button_to

@ohpreis you are not going crazy. I can confirm that data: { turbo_confirm: "Are you sure?" } also doesn’t work for me.

My system configuration is: Rails version: 7.0.2 Ruby version: 3.0.3

This really helped me!

I use Rails 7 and this really works

I know this is closed, but I’ve been fighting this whole delete tag helper since January. In the two apps I upgraded I ended up created a destroyConfirmTag helper that used stimulus to ask the confirm message.

I can confirm that both:

<%= link_to "Delete", @event, class: "button",
              data: { turbo_method: :delete, turbo_confirm: "Really?!?" } %>
and

<%= button_to "Delete", @event, class: "button", 
            form: {
                  data: { turbo_method: :delete, turbo_confirm: "Really?!?" } 
             } %>

work in the latest version of rails. I was just kind of lucky to find the button_to needed the form: key wrapping. But:

  1. Rails 7.0.3 scaffold now generates button_to links (without confirm tags, which are not documented)
  2. Getting started guide does not talk about button_to
  3. The redirect 303 stuff is not well known but it can get you ( I had all records in one model deleted!!!)
  4. I sense a move to button_to