Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a link to delete action in Rails (using paths and link_to)?

So far I've managed to come up with this piece of code:

  1   <% @projects.each do |p| %>
  2       <%= link_to p.name, project_path(p) %>
  3       <%= link_to "edit", edit_project_path(p) %>
  4       <%= link_to "delete", project_path(p), :method => :delete %>
  5   <% end %>

When I click on 'delete' link it takes me to destroy.html.erb, not delete.html.erb. It also communicates with controllers 'destroy' action, not with 'delete' action. In addition, the URL looks like this /projects/1 (no /delete or /destroy). What is wrong here? Is this the best way to create delete link?

like image 759
oFca Avatar asked Jan 20 '26 22:01

oFca


1 Answers

This is the correct behavior of Rails. The :delete method links to the destroy action and adds nothing to the URL -- the proper action is connected by the HTTP verb. For more information on the Rails routing structure and defaults, check out the Rails routing guide.

like image 120
Veraticus Avatar answered Jan 23 '26 12:01

Veraticus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!