Rails 3) Delete, Destory, and Routing
Posted
by
Maximus S
on Stack Overflow
See other posts from Stack Overflow
or by Maximus S
Published on 2012-12-09T04:00:53Z
Indexed on
2012/12/09
5:04 UTC
Read the original article
Hit count: 198
The problem is the code below
<%= button_to t('.delete'), @post, :method => :delete, :class => :destroy %>
My Post model has many relations that are dependent on delete. However, the code above will only remove the post, leaving its relations intact. The problem is that methods delete and destroy are different in that method delete doesn't instantiate the object.
So I need to use "destroy" instead of "delete" my post.
<%= button_to t('.delete'), @post, :method => :destroy %>
gives me routing error.
No route matches [POST] "/posts/2"
<%= button_to t('.delete'), @post, Post.destroy(@post) %>
deletes the post without clicking the button.
Could anyone help me with this?
UPDATE:
application.js
//= require jquery
//= require jquery-ui
//= require jquery_ujs
//= require bootstrap-modal
//= require bootstrap-typeahead
//= require_tree .
rake routes
DELETE (/:locale)/posts/:id(.:format) posts#destroy
Post model
has_many :tag_links, :dependent => :destroy
has_many :tags, :through => :tag_links
Tag model
has_many :tag_links, :dependent => :destroy
has_many :posts, :through => :tag_links
Problem: When I delete a post, all the tag_links are destroyed but tags still exist.
© Stack Overflow or respective owner