Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources A Route to a Delete Action Deleting Pages

Karrtik Iyer
Karrtik Iyer
3,738 Points

Instead of method:delete in erb can we use delete_path or destory_path?

Instead of below code:

<%= link_to 'Delete', @page, method: :delete %>

can we also do something like below?

<%= link_to 'Delete', page_delete_path(@page)%>

And in routes file we set up the #destroy with this path

delete '/pages/:id', to:'pages#destroy', as:'page_delete'

1 Answer

Jay McGavren
Jay McGavren
Treehouse Teacher

This line in your routes.rb:

delete '/pages/:id', to:'pages#destroy', as:'page_delete'

...sets up a route that will only be triggered by delete requests, which means you need that method: :delete in your ERB. You could make this work by creating a get route (with a totally separate path like "/pages/:id/delete", not "/pages/:id"), but that breaks convention and I would strongly recommend against it.