link_to delete url is not working
Asked Answered
W

8

23

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.

Waldman answered 12/12, 2010 at 18:45 Comment(2)
What is your route (routes.rb) for blogs?Preceding
try type 'rake routes' and show the output ...Katha
S
22

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.

Sumbawa answered 20/12, 2010 at 4:40 Comment(1)
@Waldman hey if you think this answer was rockin, think you could hook it up with an accepted answer? =) thxxxxSumbawa
I
18

Make sure these lines appear in application.js :

 //= require jquery
 //= require jquery_ujs
Iodoform answered 3/8, 2012 at 10:12 Comment(0)
S
5

Ensure that you have java script turned on. Otherwise :method => :delete acts just as show in Rails.

Storey answered 12/12, 2010 at 18:48 Comment(2)
then you should verify that you have rails.js loaded, and no javascript errors on the page.Gimcrackery
@Felix: if you don't see "Are you sure?" when you click on this link, then something with js is wrong.Storey
A
4

If you're using restful routing for blogs, then the following should work:

<%= link_to "Delete", @blog, :method => :delete, :confirm => "Are you sure ?"%>
Acidify answered 12/12, 2010 at 19:7 Comment(0)
M
4

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>
Monomer answered 5/9, 2011 at 0:45 Comment(0)
S
3

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.

Simplicidentate answered 8/2, 2016 at 14:9 Comment(0)
D
0

you should use

<%=button_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>  
Deckle answered 24/11, 2012 at 17:51 Comment(0)
P
0

It's possible to have a working link_to without jQuery

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://mcmap.net/q/543571/-ruby-on-rails-link_to-delete-method-not-working

But for making this easy, I'm writing this here again.

We need to work with 3 things:

  1. Adding destroy method in articles_controller.rb
  2. routes.rb setup
  3. LINK_TO tag

Let's Start...

Adding destroy method in articles_controller.rb

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

  1. in the 1st line, we're calling a variable '@article' which will find and select the ID parameters of the article on rails console from our database. Then,
  2. in 2nd line, the @article variable will make the destroy command in the console. then,
  3. in 3rd line: the id params will be deleted and
  4. in 4th line, a notice will flash in application page "Art has been deleted" and also will show in console that, found nothing in the database.
  5. In 5th line, after the destroying process completed, we will be redirected to the article index page.

This is the main factor which will give the destroying command. and make the link_to working.

Setup routes.rb

BUT WAIT We need 2 routes for the destroy page which are:

  1. A GET protocol setup
  2. A DELETE protocol setup

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

  1. The 2nd last line is declaring the DELETE method,

    • 'articles/:id/delete' will be the link structure in post link tag (known as: anchor tag in HTML) for every single post,
    • '=>' is pointing the link structure to the controller tag which is 'articles#destroy',
    • then we defined the path text by setting ** as: 'articles_delete'** which we will use as: 'articles_delete_path' or 'articles_delete_url' in link_to tag.

Then

  1. in last line, we defined the get request for the delete link which will give us a working link like "https://2haas.com/articles/1/delete" except "/articles/1/destroy" that means we can customize our delete link from this 2 methods setup with more additional information..

Last sweetest delete output

The desired link_to tag

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

Preterit answered 27/5, 2021 at 2:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.