Reputation: 178
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?
Upvotes: 6
Views: 3664
Reputation: 389
If your problem is not having AJAX request you have to add proper javascript tags
Upvotes: -1
Reputation: 16394
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
Upvotes: 6
Reputation: 178
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.
Upvotes: 0
Reputation: 20240
From my code:
<%= link_to_remote "Delete", :url => post_url(post), :method => :delete %>
Upvotes: 4