-1

For my blog, the delete method doesn't work (edit, update, create... are fine).

I already tried different ways of defining the link, but it all didn't help yet. Now at the moment, my html.erb code looks like the following:

<div class="btn">
<%= link_to "Delete", post_path(@post), :confirm => "Are you sure?", :method => :delete %>
</div>

And the controller like this:

def destroy
    @post.destroy
    redirect_to post_path
end

Rake routes:

                    posts GET    /posts(.:format)                    posts#index
                          POST   /posts(.:format)                    posts#create
                 new_post GET    /posts/new(.:format)                posts#new
                edit_post GET    /posts/:id/edit(.:format)           posts#edit
                     post GET    /posts/:id(.:format)                posts#show
                          PATCH  /posts/:id(.:format)                posts#update
                          PUT    /posts/:id(.:format)                posts#update
                          DELETE /posts/:id(.:format)                posts#destroy
4

3 回答 3

0

请尝试添加一种机制来查找@post,因为@post需要分配 http 是无状态的。@post = Post.find(params[:id])已添加部分供您尝试。谢谢你。

def destroy
  @post = Post.find(params[:id])
  @post.destroy
  redirect_to posts_path
end
于 2016-07-20T14:13:22.170 回答
0

试试这个查询的链接:

<div class="btn">
<%= link_to "Delete", @post, :confirm => "Are you sure?", :method => :delete %>
</div>

我认为问题出在 post_path(@post) 上。希望这行得通。

于 2016-07-20T10:00:00.377 回答
0

如果路由资源丰富并且数据库中发生删除,则问题可能是redirect_to路由不是复数。

见: http: //guides.rubyonrails.org/routing.html#path-and-url-helpers

更正:

def destroy
  @post.destroy
  redirect_to posts_path
end
于 2016-07-20T11:41:19.893 回答