Reputation: 11542
I have the following link_to delete url in my app
<%=link_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>
It does not seem to be working.When I click this url, it just takes me to the show path.Can someone please tell me how to fix this. Thanks.
Upvotes: 23
Views: 33005
Reputation: 419
I've found the best process to make a working delete link for ruby on rails without jQuery! Here I already make an answer: https://stackoverflow.com/a/67710994/14387700
But for making this easy, I'm writing this here again.
We need to work with 3 things:
Let's Start...
At first, we will add def destroy ... end
in articles_controller.rb
,
lets open:
# app/controllers/articles_controller.rb
def destroy
@article = Article.find(params[:id])
@article.destroy
params[:id] = nil
flash[:notice] = "Art has been deleted"
redirect_to :action => :index
end
Here
This is the main factor which will give the destroying command. and make the link_to working.
BUT WAIT We need 2 routes for the destroy page which are:
In routes just add:
resources :articles, except: [:destroy] # this will add all get request links automatically except destroy link
post '/articles/new' => 'articles#create'
post '/articles/:id' => 'articles#update'
post '/articles/:id/edit' => 'articles#update' # this 3 lines are needed for other forms purpose
# we need this 2 lines for our delete link_to setup
delete 'articles/:id/delete' => 'articles#destroy', as: 'articles_delete'
get '/articles/:id/delete' => 'articles#destroy'
Here
The 2nd last line is declaring the DELETE method,
Then
Last sweetest delete output
we can use this to get proper delete link_to tag which will work.
<%= link_to 'Delete Article', articles_delete_path, method: :delete %>
<%= link_to 'Delete Article', articles_delete_url, method: :delete %>
<% obj.each do |post| %>
<%= link_to 'Delete Article', articles_delete_path(post), method: :delete %>
<% end %>
AND that's done except jQuery
Thanks for reading this answer properly.!
HAPPY PROGRAMMINGS
Upvotes: 0
Reputation: 20033
In order for link_to
to work with the delete
method, Rails needs the unobtrusive scripting adapter for jQuery.
Make sure that your Gemfile has
gem 'jquery-rails'
Make sure that app/assets/javascripts/application.js has
//= require jquery
//= require jquery_ujs
Make sure that your app/views/layouts/application.html.erb has
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
inside the head
tag. Remove the 'data-turbolinks-track' => true
section if you don't plan to use Turbolinks.
Upvotes: 3
Reputation: 7289
Make sure these lines appear in application.js
:
//= require jquery
//= require jquery_ujs
Upvotes: 18
Reputation: 1025
Are you using jQuery? If so, I think the problem could be that you are using jQuery without the updated rails.js file.
Download rails.js here: https://github.com/rails/jquery-ujs/raw/master/src/rails.js Drop it in your javascripts directory, overwriting the rails.js that comes default with rails.
Add a javascript include line to include it.
<%= javascript_include_tag "rails" %>
Put this after your Jquery include tag. You probably also want to disinclude the javascript defaults if you don't plan on using prototype.
I included jQuery UI in my application, I found that delete is now working as show, but after doing above Resolved Issue.
Upvotes: 22
Reputation: 7378
you should use
<%=button_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>
Upvotes: 0
Reputation: 9693
You can try with 'data-method' instead of :method.
<%=link_to "Delete",blog_path(@blog.id), 'data-method' => :delete, :class => "delete", :confirm => "Are you sure ?"%>
You can check on jquery_ujs.js the following piece of code:
// Handles "data-method" on links such as:
// <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
Upvotes: 4
Reputation: 2525
If you're using restful routing for blogs, then the following should work:
<%= link_to "Delete", @blog, :method => :delete, :confirm => "Are you sure ?"%>
Upvotes: 4
Reputation: 14967
Ensure that you have java script turned on. Otherwise :method => :delete
acts just as show in Rails.
Upvotes: 5