Upon clicking delete, rails refreshes page but does nothing - ruby-on-rails

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?" } %>

Related

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
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.

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.)

How to call a certain path in delete method Rails

Typically when I am using a delete link it will look something like this
<% #variable_name.each do |block| %>
<%= link_to 'Destroy', block, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
For the current project, I am unable to use the "block" path for delete. I now need to call a path that is related to block, but not defined in it. I was thinking something like this
<% #variable_name.each do |block| %>
<%= link_to 'Destroy', someother_path, method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
Is it possible to link to a url instead of using a helper method? If so how would I find that url.
Yes, you can use any other urls as long as you defined the routes in routes.rb
in your routes.rb file
resources :variables do
delete :someother, on: :member
end
then you can check the routes.
To check the urls, please run following command line in terminal.
rake routes
or
rake routes | grep variable
and you can do
<% #variable_name.each do |block| %>
<%= link_to 'Destroy', someother_variable_path(Variable.find_by_name(block)), method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
You can pass arguments to the path helper:
someother_path(first_argument: 'whatever')
Also, if you want the full path of the link (including domain host + port), use this:
someother_url(first_argument: 'whatever')
The last option is to write the path as a string, for example:
link_to 'Destroy', "/posts/#{post.id}/destroy" # etc.
But this last option should not be used as it is complicated to maintain.

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