Perform AJAX removal operations on rails

How do you perform removal and railing operations? I read the documentation and thought that I was doing everything right, but I can not get it to work.

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

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

In what I want to call the Ajax operation, I should only have a line:

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

When I click on the link, it still sends the POST operation, so it does nothing.

What am I missing or is something wrong?

+6
rest ruby-on-rails
source share
4 answers

Try

 :url => employee_url(@employee) 

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

+6
source share

From my code:

 <%= link_to_remote "Delete", :url => post_url(post), :method => :delete %> 
+4
source share

Just add a few additional details: using :url => employee_url(@employee) helped (from the accepted answer). The other part that messed me up was that I was expecting an HTTP delete request, but I continued to receive POST requests with the "_method" parameter (automatically added by the rails) that was set to delete.

Thus, it triggered the proper kill action, which I proved by adding a couple of debug commands to the controller. Yes, my delete code was wrong in the controller, so it wasn’t deleted when I thought it was.

0
source share

If your problem does not have an AJAX request, you need to add the appropriate javascript tags

-one
source share

All Articles