How to destroy a nested resource instance in Ruby on Rails? - 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?'} %>

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.

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

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

Polymorphic Association - Deleting comments

I am creating an application that allows users to make comments on project postings created. I followed this Railscast to set up polymorphic associations.
Everything works great per tutorial, but I have been trying to build in a delete method in my comment controller so comments can be deleted. I created a method in my comments controller called destroy.
Comments are made on my project postings. The partial for the comments are generated on the project pages and on the pages of the users who make the postings. When I delete on the project pages, it works great, but when I try to delete from the user page, I get the result below. How can I fix it so comments can be deleted by users whether they are deleting from the project page or from their own user pages?
Unknown action
The action 'show' could not be found for CommentsController
comments_controller.rb
def destroy
#comment = Comment.find(params[:id])
#comment.destroy
if #comment.destroy
redirect_to #commentable, notice: "Comment deleted."
end
end
_comment.html.erb
<div class="comments">
<p><%= comment.content %></p>
<span>
By <%= link_to comment.user.name, comment.user %> <%= time_ago_in_words(comment.created_at) %> ago
<div class="pull-right">
<%= link_to "Destroy", [#commentable, comment], method: :delete, data: { confirm: 'Are you sure?' } %>
</div>
</span>
</div>
routes.rb
resources :comments
resources :projects do
resources :comments
member do
get :following
end
end
Try this...
<%= link_to "Destroy", [#commentable, comment], method: :delete, data: { confirm: 'Are you sure?' } %>

Resources