bhanu bhanu - 5 months ago 33
Ruby Question

How do I specify destroy path helper in a view of my Rails app?

I have a model Category in my Rails app. According to rails RESTful Routes, I can perform CRUD Operations on model, having

resources: categories
defined in my routes.rb.

But how do i define destroy path helper in my view to perform
DELETE
action, just like
edit_category_path(@category)
to edit the record. I tried like this
destroy_category_path(@category)
but getting error as

undefined method `destroy_category_path' for #<#<Class:0x00000005371298>:0x000000053734f8>

Answer

The path is exactely the same as for show action, the only idfference is that you need to use DELETE verb:

button_to @category, method: :delete

Note, it is not considered safe to use links having destructive/constructive actions, as those might be visited by robots.