Вопрос

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.

Это было полезно?

Решение

:method => :delete means the HTTP METHOD DELETE, nothing related with the delete method of active record.

You need to check your code of your models, have you missed the :dependent => :destroy options of the relations?

For example, if the post has many comments, then it should be:

has_many :comments, :dependent => :destroy

Of course, in your controller, you need to use @post.destroy instead of @post.delete.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top