Andrzej Gis
Andrzej Gis

Reputation: 14306

ROR ways of calling destroy action

I ask this question only because I'm curious.

The first line is standard, RESTful and of course invokes destroy method. The second line returns a routing error.

The question is how to repair the second line to work like the first one. As I mentioned before, It's just for my curiosity and better ROR understanding.

<%= link_to 'delete', file, :method => :delete %></td>
<%= link_to 'delete', {:action => :destroy, :controller => 'files', :id => file.id.to_s}, :method => :delete %>

Bye

Upvotes: 2

Views: 1733

Answers (1)

Chris Barretto
Chris Barretto

Reputation: 9529

Disclaimer: I know this is not the way to do a delete. It is proof of concept on just how to access an action via a controller outside of REST.

You can do this:

<%= link_to 'delete', {:action => :destroy, :controller => 'files', :id => file.id} %>

You don't need the method destroy if you explicitly access the action. Also don't need the to_s for the file.id

Upvotes: 1

Related Questions