bhanu
bhanu

Reputation: 2460

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>

Upvotes: 4

Views: 7678

Answers (2)

Leo Holman
Leo Holman

Reputation: 11

There is a path helper for delete, but Rails defaults to having this route not defined. To activate the helper you need to add delete to your resourceful routes in your routes.rb file.

resources :categories do
    member do
       get :delete
    end
end

Once you've done that you should be able to use delete_category_path(@category).

Then inside your category you can call destroy on the object from your delete action.

Upvotes: -1

BroiSatse
BroiSatse

Reputation: 44675

The path is exactly the same as the show action ('/categories/:id'), but you also need to specify the DELETE HTTP method:

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.

Upvotes: 9

Related Questions