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

Time:05-30

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

  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

CodePudding user response:

Use the following

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