Rails 7 "delete method" and "No route matches [GET]" - ruby-on-rails

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
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
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
this is my routes
# config/routes.rb
namespace :api do
resources :posts, only: [:create, :update, :destroy]
end
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

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

Just a quick note if you want to delete something with a link in rails 7:
<%= link_to 'Delete', api_post_path(draft),
data: {turbo_method: :delete, turbo_confirm: 'Are you sure?'} %>
This link would send a real DELETE request.
If your destroy action ends with a redirect_to (which I doubt is your case), some browsers will redirect to a new location with DELETE method (causing errors), so make sure to add status: :see_other parameter to redirect_to, like the guides suggest.

Related

No route matches [DELETE] "/tags"

Edited to include config/routes.rb file.
I'm working through the Jumpstartlab Blogger 2 tutorial and I've run into trouble trying to delete tags from a tag list. This is my first Rails project and I'm still trying to wrap my head around MVC and routing.
Here's the code from my Tags view:
<h1>All Tags</h1>
<ul id="tags">
<% #tags.each do |tag| %>
<li>
<%= link_to tag.name, tag_path(tag), class: 'tag_name' %>
<%= link_to "Delete", tags_path(#tag), method: :delete, data: {confirm: "Really delete the tag?"} %>
</li>
<% end %>
</ul>
And the code from my Tags controller:
class TagsController < ApplicationController
def show
#tag = Tag.find(params[:id])
end
def index
#tags = Tag.all
end
def destroy
#tag = Tag.find(params[:id])
#tag.destroy
end
end
And config.routes.rb:
Blogger::Application.routes.draw do
root to: 'articles#index'
resources :articles do
resources :comments
end
resources :tags
end
The error I'm getting is No route matches [DELETE] "/tags".
I feel like the issue is something basic that I haven't quite grasped yet. I would really appreciate if someone could help me understand what I've missed and how it works. If I haven't provided enough information, please let me know. And thanks!
It's because the DELETE route is based on an instance (just 1 tag) of a resource, not the collection (the group of tags).
So you have to change this line:
<%= link_to "Delete", tags_path(#tag), method: :delete, data: {confirm: "Really delete the tag?"} %>
To use tag_path(tag):
<%= link_to "Delete", tag_path(tag), method: :delete, data: {confirm: "Really delete the tag?"} %>

Getting no route matches error despite having "resources" in my config/routes.rb file

I"m using Rails 4.2.3 and I’m getting a “No route matches {:action=>"delete", :controller=>"my_objects", :id=>8}” error when I visit my view which contains the below link
<%= link_to 'Delete',url_for(controller: 'my_objects', action: :delete,id: my_object_time.my_object.id),method: :delete, data: {confirm: "Are you sure you want to delete this data?"} %>
However, in my routes/config.rb file, I have included
resources :my_objects
I even have the method in my my_objects_controller.rb file …
def delete
my_object.find(params[:id]).destroy
format.html { redirect_to controller: "users", action: "index", notice: 'Delete successful.' }
end
So I’m not sure why its all falling apart. Any help is appreciated, - Dave
try this:
<%= link_to 'Delete',url_for(controller: 'my_objects', action: :destroy,id: my_object_time.my_object.id),method: :delete, data: {confirm: "Are you sure you want to delete this data?"} %>
or
<%= link_to 'Delete', my_object_time.my_object.id, method: :delete, data: { confirm: 'Are you sure?' } %>

Destroy method in rails and Mongoid

I am trying to delete a particular "work" from an array of "works" which is embedded in user.
In my work.html.erb file in views:-
<% #works.each do |f| %>
<%= link_to 'Destroy', profiles_destroy_path(f), data: {:confirm => 'Are you sure?'}, :method => :delete %>
<% end %>
And in my controller:-
def destroy
#work = current_user.works.find(params[:id])
#work.destroy
respond_to do |format|
format.html { redirect_to root_url }
end
end
I am getting following error:-
Mongoid::Errors::InvalidFind at /profiles/destroy.56fa4d2f498b5908a002e2e8
P.S. - I am new to rails.
Please check params[:id] is not nil. There's no :id at session start and you get the Mongoid::Errors::InvalidFind exception above.
delete action doesn't have path by default..
<%= link_to 'Destroy', profiles_path(f), data: {confirm: 'Are you sure?'}, method: :delete %>
That will call the destroy action from your controller.
And make sure params[:id] is not nil.
PS: use rails 4 annotation (avoid using rockets (=>))
There may be an error in Juan's answer.
I believe you want:
<%= link_to 'Destroy', profile_path(f), method: :delete, data: {confirm: 'Are you sure?'} %>
Note: the singular "profile_path" instead of "profiles_path"
This is because the destroy action uses the same path as the show action, but uses a DELETE method instead of a GET. (Which you can observe by running "rake routes" in your terminal and comparing the two routes.)

Upon clicking delete, rails refreshes page but does nothing

I am writing a rails app for planning school assignments.
I have been experiencing a problem upon clicking the delete button on the web page it just refreshes the page but doesn't delete anything from the page.
Here is my destroy action from assignment_controller along with assignment_params.
def destroy
#assignment = Assignment.find(params[:id])
#assignment.destroy
redirect_to assignments_path
end
private
def assignment_params
params.require(:assignment).permit(:name)
end
And this is my index.html.erb
<h1>Listing of Assignments</h1>
<% #assignment.each do |x| %>
<h2><%= x.name %></h2>
<%= link_to "Delete", assignments_path(#assignment),
method: :delete,
data: { confirm: "Are you sure?" } %>
<% end %>
I'm not sure if the problem is with the route but here is my routes.rb just in case.
Rails.application.routes.draw do
devise_for :users
resources :assignments
root 'home#index'
end
Thank you!
Instead of:
<%= link_to "Delete", assignments_path(#assignment),
method: :delete,
data: { confirm: "Are you sure?" } %>
Try this:
<%= link_to "Delete", assignment_path(x),
method: :delete,
data: { confirm: "Are you sure?" } %>
You must pass the x not #assignment because you have been passed it in a loop. Also, change: assignments_path to assignment_path.
You have to change your link for delete to below
<%= link_to "Delete", x, method: :delete, data: { confirm: "Are you sure?" } %>

How to destroy a nested resource instance in Ruby on Rails?

I have nested resources:
#Nesting Resources
resources :users do
resources :photos do
resources :tags
end
end
In my view, I want to have a link to delete tags, but when clicking on it, the Photo is deleted instead.
<% if #photo.user === current_user %>
<%= link_to "Eliminar", [#tag.photo.user,#tag.photo, #tag.id] , method: :delete, data: { confirm: 'Quieres borrar esto?'} %>
<% end %>
UPDATE
This is where I need to get:
DELETE /users/:user_id/photos/:photo_id/tags/:id(.:format) tags#destroy
Thanks!
Try this
<%= link_to "Eliminar", "/users/#{#tag.photo.user.id}/photos/#{#tag.photo.id}/tags/#{#tag.id}" , method: :delete, data: { confirm: 'Quieres borrar esto?'} %>

Resources