Reputation: 5455
I am not able to find the right way to destroy a record. I feel like such a complete newb.
Here are the routes pertaining to the controller (output from rake routes
):
contents GET /admin/contents(.:format) {:controller=>"contents", :action=>"index"}
contents POST /admin/contents(.:format) {:controller=>"contents", :action=>"create"}
new_content GET /admin/contents/new(.:format) {:controller=>"contents", :action=>"new"}
edit_content GET /admin/contents/:id/edit(.:format) {:controller=>"contents", :action=>"edit"}
content GET /admin/contents/:id(.:format) {:controller=>"contents", :action=>"show"}
content PUT /admin/contents/:id(.:format) {:controller=>"contents", :action=>"update"}
content DELETE /admin/contents/:id(.:format) {:controller=>"contents", :action=>"destroy"}
What is getting me is the bottom line does not look any different than the get and put.
Here is the link:
<%= link_to 'Destroy', content, :confirm => 'Are you sure?', :method => :delete %>
also tried:
<%= link_to 'Destroy', content, :confirm => 'Are you sure?', :method => :destroy %>
and the output is:
<a href="/admin/contents/1400" data-confirm="Are you sure?" data-method="destroy" rel="nofollow">Destroy</a>
Can someone spot what I am doing wrong here? :-/
edit
I did not intially have rails.js loading. I do now.
Here is the contents of my destroy action:
def destroy
@content = Content.find(params[:id])
@content.destroy
respond_to do |format|
format.html { redirect_to(contents_url) }
format.xml { head :ok }
end
end
I am deleting from the content index, like so:
<% @contents.each do |content| %>
<tr>
<td><%= content.name %></td>
<td><%= link_to 'Show', content %></td>
<td><%= link_to 'Edit', edit_content_path(content) %></td>
<td><%= link_to 'Destroy', content, :confirm => 'Are you sure?', :method => :destroy %></td>
</tr>
<% end %>
</table>
Upvotes: 0
Views: 2775
Reputation: 25377
The URL looks the same because it is the same. The difference lie within the request method. Your Rails app knows to separate GET
, PUT
and DELETE
requests--even if they are made to the same URL--and route the request to the right action.
However, not all browsers/web servers support all of these methods, so Rails rely upon unobtrusive JavaScript (ujs) to "fake" some of the requests--more specifically, PUT
and DELETE
. Because of this, you'll need to include one of the bundles for Rails apps (the Prototype comes by default; you can get the jQuery version through this gem). You can find out more through the README (and of course the source) of the jQuery ujs.
If you're experiencing issues, it's probably because you don't have the necessary ujs. It could also be that you haven't included the csrf_meta_tag
in your html header.
Upvotes: 4
Reputation: 80041
A few things could be going wrong, but it's hard to narrow down without more information on what you have and what errors/behavior you're getting.
data-method="delete"
isn't having the effect of sending an AJAX post with a _method
argument set to "delete". destroy
controller action isn't doing what you are expecting.content
is not a local variable referring to an instance of the model you are trying to destroy (note that you would only use the singular content_path
if this were a singular resource you were trying to destroy). Are you looking for @content
instead maybe? It's hard to say without some context.And just to clarify for you, the method
option is the HTTP method, not the controller method. There is no destroy
HTTP method; Rails just uses destroy
over delete
as a sort of "delete gracefully".
Upvotes: 2