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)
For anyone else coming across this, this solved the issue for me.
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,
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:
work in the latest version of rails. I was just kind of lucky to find the button_to needed the form: key wrapping. But:
@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!