Rails - delete method not working - ruby-on-rails

I am trying to delete an entry as specified in the docs but I keep getting en error stating NoMethodError in Tasks#show and the entry is not deleted.
index.html.erb:
<%= link_to 'Delete', destroy_task_path(task['id']), data: { confirm: 'Are you sure?' } %>
route.rb:
delete '/tasks/:id', to: 'tasks#destroy', as: 'destroy_task'
resources :tasks
root 'home#index'
tasks_controller.rb
def destroy
uri = URI.parse("http://localhost/tasks/public/api/tasks/"+params[:id])
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Delete.new(uri.path)
redirect_to :tasks, notice: 'Task was successfully destroyed.'
end
What am I doing wrong here?! and why does it get redirected to show?!

You missed in your link_tocall the method: :delete, otherwise you do a GET call.

Related

In Rails 6, a link for delete without resource route is not working

For a CRUD operation in Rails 6, I am not using the resource route. I write an html link to delete a post with <a></a> tag. Instead of delete, this link redirects to the show page with a get request.
Here is the link
<a href="/posts/<%= post.id %>" data-method="delete" >Delete</a>
Here is the routes.rb file
Rails.application.routes.draw do
get '/posts', to: 'posts#index'
get '/posts/new', to: 'posts#new'
post '/posts', to: 'posts#create'
get '/posts/:id', to: 'posts#show'
get '/posts/:id/edit', to: 'posts#edit'
patch '/posts/:id', to: 'posts#update'
delete '/posts/:id', to: 'posts#destroy'
end
Here is the posts_controller.rb
def destroy
#post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: "Post was successfully destroyed." }
format.json { head :no_content }
end
end
Here is the application.js file
require("#rails/ujs").start()
require("turbolinks").start()
require("#rails/activestorage").start()
require("channels")
Here is the rails way to create an a link to a delete method:
<%= link_to "delete", your_posts_path(id), method: :delete, data: { confirm: "You sure?" } %>

Routing Error No route matches [DELETE] "/topics"

Trying to get delete action for topics controller to work properly.
Here is my topics controller
def destroy
#topic = Topic.find(params[:id])
if #topic.destroy
flash[:notice] = "\"#{#topic.title}\" Topic was deleted!"
redirect_to topics_path(#topic)
else
flash.now[:alert] = "An error occurred. Please try again."
render :show
end
end
My routes:
resources :topics do
resources :bookmarks, except: [:index]
end
My delete link on my index view:
<%= link_to "Delete Topic", #topic, method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>
Really don't understand what I am missing.
Try with these changes:
def destroy
#topic = Topic.find(params[:id])
if #topic.destroy
flash[:notice] = "\"#{#topic.title}\" Topic was deleted!"
redirect_to topics_path
else
flash.now[:alert] = "An error occurred. Please try again."
render :show
end
end
Delete Link:
<%= link_to "Delete Topic", topic_path(#topic), method: :delete, class: 'btn btn-danger', data: { confirm: 'Are you sure you want to delete this topic?' } %>

Delete method in ROR route error when using remote true

I'm trying to delete post with the remote: true command in rails. Everything is working fine when i'm not using ajax. But when i use the remote true command i get an routing error.
view:
<% #posts.each do |post| %>
<%= post.title %>
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
<% end %>
controller:
def index
#posts = Post.all
end
def destroy
#post = Post.find(params[:id])
#post.destroy
redirect_to :back
end
routes:
resources :posts
This is the error i get in the log.
Started DELETE "/posts" for 127.0.0.1 at 2014-10-11 12:46:33 +0200
ActionController::RoutingError (No route matches [DELETE] "/posts"):
Thanks in advance.
Update. I get this when i write rake routes.
DELETE /posts/:id(.:format) posts#destroy
To get this to working.
Change:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
redirect_to :back
end
To this:
def destroy
#posts = Post.find(params[:id])
#posts.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.js { head :no_content }
end
end
and:
<%= link_to 'delete', post_path(post), method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
to:
<%= link_to 'delete', post, method: :delete, data: { confirm: 'Are you sure?' }, remote: true %>
Incase anyone is having any problems with this, I managed to solve it by adding :data => { :type => :json } to the link.
For example:
<%= link_to "Delete this post", #post, :method => :delete, :remote => true, :data => { :type => :json } %>
I imagine UJS is expecting HTML, however JSON is returned so it fails and posts again (I noticed two requests).
Hope this helps!

RoR Empty Cart/Delete line_Items

I'm still pretty new to RoR and have run into a problem I can't seem to solve. I've already built the functions to add new products to the database and allow a user to add individual items to their cart, my delete method just gives me an error saying it can't find the delete method in the carts_controller. Any help would be appreciated.
\app\views\cart\show.html.erb
<%= button_to 'Empty Cart', #cart, method: :delete,
data: { confirm: 'are you sure?'} %>
\app\controllers\carts_controller.rb
def destroy
#cart.destroy if #cart.id == session[:cart_id]
session[:cart_id] = nil
respond_to do |format|
format.html { redirect_to store_url,
notice: 'Your cart is empty' }
format.json { head :no_content }
end
end
private
def set_cart
#cart = Cart.find(params[:id])
end
Error Message Given
Unknown Action
The action 'destroy' could not be found for CartsController
\config\routes.rb
Depot::Application.routes.draw do
root 'store#index', as: 'store'
resources :line_items
resources :carts
get "store/index"
resources :products
end
Depends on what is currently set in your #cart variable, can you put out an inspect for that?
Otherwise, you can also try something like this:
<%= link_to 'Empty Cart', cart_path(#cart), method: :delete, confirm: 'are you sure?' %>

No route matches [POST] "/coachings/4" when trying to use Destroy

coachings GET /coachings(.:format) {:action=>"index", :controller=>"coachings"}
POST /coachings(.:format) {:action=>"create", :controller=>"coachings"}
new_coaching GET /coachings/new(.:format) {:action=>"new", :controller=>"coachings"}
edit_coaching GET /coachings/:id/edit(.:format) {:action=>"edit", :controller=>"coachings"}
coaching GET /coachings/:id(.:format) {:action=>"show", :controller=>"coachings"}
PUT /coachings/:id(.:format) {:action=>"update", :controller=>"coachings"}
DELETE /coachings/:id(.:format) {:action=>"destroy", :controller=>"coachings"}
my routes are correct, here is my view index
<%= link_to 'Destroy', coaching, :confirm 'Are you sure?', :method => :destroy %>
here is my controller
def destroy
#coaching = Coaching.find(params[:id])
#coaching.destroy
respond_to do |format|
format.html { redirect_to coachings_path }
format.json { head :ok }
end
end
any ideas why i get this error? i'm new to RoR this is my first projects i've done by myself.
Use :delete method
<%= link_to 'Destroy', coaching, :confirm => 'Are you sure?', :method => :delete %>
In your link_to you are using a method of destroy which isn't a valid HTTP verb so Rails is probably defaulting to POST. You'll need to use DELETE instead:
<%= link_to 'Destroy', coaching, confirm: 'Are you sure?', method: :delete %>

Resources