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>
The URL looks the same because it is the same. The difference lie within the request method. Your Rails app knows to separate
GET,PUTandDELETErequests–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,
PUTandDELETE. 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_tagin your html header.