Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rails 7 "delete method" and "No route matches [GET]"

I have the below script for deleting the draft posts

<%= link_to "Delete", api_post_path(draft), method: :delete, 
                                            remote: true %>

It works properly, but after updating the rails version to 7.0.3 it’s not working anymore.

These are my project information

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. I have the Libraries collection
# app/controllers/libraries_controller.rb

class LibrariesController < ApplicationController
  ...
  def drafts
    @drafts = current_user.posts.recent.drafts.paginate(page: params[:page])
  end
  ...
end
  1. I have this collection for deleting the draft post
# app/controllers/api/posts_controller.rb

module Api
  class PostsController < ApplicationController
    ...
    destroy
      @post = current_user.posts.friendly.find(params[:id])
      @post.destroy
    end
    ...
  end
end
  1. this is my routes
# config/routes.rb

namespace :api do
  resources :posts, only: [:create, :update, :destroy]
end
  1. View to show all draft posts list with the link for deleting the draft post
<!-- app/views/libraries/drafts.html.erb -->

<div id="library_<%= draft.id %>">
  ...
  <%= link_to "Delete", api_post_path(draft), method: :delete, 
                                              remote: true %>
  ...
</div>
<!-- app/views/api/posts/destroy.js.erb -->

$('#library_<%= @post.id %>').fadeOut();

But now it’s not working then I removed method: :delete and updated the new script

<%= link_to "Delete", api_post_path(draft), data: { 
                                                    turbo_method: "delete", 
                                                    turbo_confirm: "Are you sure?" 
                                            }, 
                                            remote: true %>

It still not working then I updated the script again by removing remote: true

<%= link_to "Delete", api_post_path(draft), data: { 
                                                    turbo_method: "delete", 
                                                    turbo_confirm: "Are you sure?" 
                                            } %>

After that, I got this error

No route matches [GET] "/api/posts/xxx"

Please advise how can I fix this issue

>Solution :

Use the following

<%= link_to "Delete", api_post_path(draft), method: :delete, data: { turbo: false } %>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading