37

I am trying to create a destroy link to my users controller, I am also using devise.

Here is my code -

View

<%= link_to 'Delete User?', child, :confirm => "Are you sure you want to delete #{child.full_name}?", :method => :delete, :class => "user-additional", :style => "font-size:12px;font-weight:normal;" %>

Controller

def destroy
 if @user = User.find(params[:id])
  @user.destroy
  respond_to do |format| 
    format.html { redirect_to account_index_path } 
    format.xml { head :ok } 
  end
 end
end

Routes

devise_for :users 
resources :users, :except => [:new]

The link translates to localhost:3000/users/10

When clicked this opens the users show instead of deleting them

Any ideas ?

Zabba
  • 64,285
  • 47
  • 179
  • 207
Alex
  • 6,205
  • 7
  • 43
  • 53
  • Do you know if the link is using a GET or DELETE request? I know you have method specified there, but when you say the link resolves to localhost:3000/users/10, that could mean a couple of things. You can check by using an inspection tool like Firebug or Chrome Inspector. – A. Wilson Jan 05 '11 at 17:23
  • Is JavaScript enabled? Is the `rails.js` file included in the application layout file? What about the prototype JS lib? If jQuery and prototype JS libs are being used, try using only prototype? – Zabba Jan 05 '11 at 17:35
  • link_to is a bad practice if it is not a get request, as it can be opened in a new tap... clicked by web crawlers ... etc user button_to instead as it creates a form – zeacuss Jul 15 '12 at 10:19
  • Probably you have wrong link to script file in head of your layout. – Alex Jun 04 '12 at 09:21

9 Answers9

64

Destructive actions should be performed as a form submission - http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist

use button_to (passing a :method => :delete) instead and style the button appropriately.

shybovycha
  • 11,556
  • 6
  • 52
  • 82
Omar Qureshi
  • 8,963
  • 3
  • 33
  • 35
  • Thank you so much! I have been 2 hours around this problem, and your answer save my work day. – Afaria Nov 24 '14 at 19:08
27

Actually I just had the exactly same problem yesterday

Try this:

<%= button_to "delete", your_object, :method=>:delete, :class=>:destroy %>

It works (for me at least)

shybovycha
  • 11,556
  • 6
  • 52
  • 82
JayX
  • 1,754
  • 1
  • 18
  • 27
  • Worked for me too but don't why its not working with link_to. jquery was automatically included by rails 4.2. I'm using ruby 2.0.0. Javascript enabled in Google chrome content settings. – learner Sep 30 '15 at 12:31
  • Did this with a nested object (passed the child_object) and it deleted the parent object, instead. – JosephK Jan 26 '16 at 13:11
9

In case that you are using jQuery instead of Prototype, you are probably missing a javascript file.

You can find details on how to add it to your project from the jquery-ujs GitHub page or from episode 205 of the Railscasts.

Florin
  • 1,844
  • 2
  • 16
  • 21
  • +1 despite this not being marked 'correct', it pointed me towards ujs, which was the last thing I need to ditch prototype for jQuery. Thanks! – Fil Apr 19 '11 at 18:00
6

At a guess I think it is because in Rails 3, unobtrusive javascript is now used for functionality such as this (Rails 2 would output a bunch of nasty inline javascript for your code, Rails 3 puts the javascript in an external file, and uses HTML5 data- attributes to interact with that.)

To solve this you need to include <%= csrf_meta_tags %> in your page header to reference the external javascript. It also deals with XSS issues.

Some details here: Delete link sends "Get" instead of "Delete" in Rails 3 view

Community
  • 1
  • 1
DanSingerman
  • 36,066
  • 13
  • 81
  • 92
  • I dont think its the js I just tried this - <%= link_to 'Delete User?', child, :method => :delete %> and no luck – Alex Jan 05 '11 at 17:24
  • @Maruccio, it is now, but that was correct then. Anyway I'll edit to keep this answer up to date. – DanSingerman Aug 08 '12 at 18:08
1
  1. follow the steps in the installation part rails/jquery-ujs
  2. add <%= javascript_include_tag "application" %> in your layout file.
Musaffa
  • 702
  • 8
  • 14
1

If you haven't included jquery and jquery-ujs in your app , the default link_to default coming with scaffold wont work!

I had the same issue.It got solved after including both these js!

SureshCS
  • 1,005
  • 12
  • 23
1

Also if you get this problem in production mode, it may be because you have not compiled the assets. See http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets

ReggieB
  • 8,100
  • 3
  • 38
  • 46
1

Worked for me with confirmation message.

<%= button_to 'Destroy', {action: :destroy, id: version.id},  onclick: 'return confirm("Are you sure?")', method: :delete %>
Deepak Kabbur
  • 774
  • 14
  • 25
1

If you are using jQuery, make sure you have something like this:

<script type="text/javascript">
  // this allows jquery to be called along with scriptaculous and YUI without any conflicts
  // the only difference is all jquery functions should be called with $j instead of $
  // e.g. $jQ('#div_id').stuff instead of $('#div_id').stuff
  var $jQ = jQuery.noConflict();
</script>
chech
  • 1,085
  • 14
  • 21
  • assignin it to jQ didnt work for me, I just called jQuery.noConflict(); and then I would use it like jQuery(document) instead of $(document) more info here: http://yehudakatz.com/2007/01/31/using-jquery-in-rails-part-i/ – marimaf May 04 '12 at 03:37
  • using the jQ=JQuery.noConflict(); makes jQuery available from $jQ instead of using $, which helps making calls to jQuery and javascript/ajax not conflict with each other. I had this same issue, and this line of code made my delete link work again. Maybe your problem was somewhere else. – chech May 07 '12 at 15:48
  • it did solve my problem. The only thing I was pointing out was that using $jQ didn't work for me. – marimaf May 07 '12 at 16:19
  • looks like my fault, I misstyped the variable name, it should be: var $jQ = jQuery.noConflict(); I'll edit the post and correct it, thanks for pointing it out. – chech May 07 '12 at 16:31