views:

1446

answers:

4

How do you perform delete and put operations restfully in rails? I have read the documentation and thought I was doing everything properly, but I can't seem to get it to work.

For example, if I wanted to delete an employee I would create a controller called "EmployeesController" and create a destroy method to perform the delete.

Then I went into the routes.rb file and entered map.resources :employees, which gives you access to the URL helper functions.

In whatever I want to call the Ajax operation from, I should just have a line like:

<%= link_to_remote "Delete", employee_path(@employee), :method => :delete %>

When I click on the link, it is still is sending a POST operation, so it does nothing.

What am I missing or doing wrong?

+5  A: 

Try

:url => employee_url(@employee)

IIRC, *_path is a named route generated by the :resource directive which includes the method, thus overwriting your :method => :delete

MattW.
+3  A: 

From my code:

<%= link_to_remote "Delete", :url => post_url(post), :method => :delete %>
flitzwald
A: 

Just to add a few extra details: Using :url => employee_url(@employee) helped (from the accepted answer). The other part that was messing me up was the fact that I was expecting an HTTP delete request, but I kept getting POST requests with a parameter "_method" (automatically added by rails) which was set to delete.

So it was calling the proper destroy action, which I proved by adding a couple of debug statements to the controller. Yes, my delete code was wrong in the controller, so it wasn't really deleting when I thought it was.

Chris R.
A: 

If your problem is not having AJAX request you have to add proper javascript tags