Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails 3 - link_to to destroy not working

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 ?

like image 797
Alex Avatar asked Jan 05 '11 17:01

Alex


3 Answers

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.

like image 51
Omar Qureshi Avatar answered Nov 17 '22 04:11

Omar Qureshi


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)

like image 25
JayX Avatar answered Nov 17 '22 03:11

JayX


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.

like image 9
Florin Avatar answered Nov 17 '22 03:11

Florin