我无法销毁具有有效路线的对象。浏览器返回No route matches [POST] "/blog/topics/3/posts/1"
. 但是,我可以对同一资源执行所有其他操作。鉴于我可以从控制台创建和销毁对象,我的控制器、模板应该如何?
节省时间 - 这些路线在我当前的配置下也不起作用:
- link_to([@topic, @post]) 返回http://localhost:3000/blog/topics/3/1/posts/1。
- link_to([@topic]) 返回http://localhost:3000/blog/topics/3/posts/1。#依然不删除
这是我的控制器:
class Blog::PostsController < ApplicationController
before_filter :fetch_topic, except: [:index]
before_filter :fetch_post, except: [:create, :new]
#stuff that works.
..
..
..
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to blog_topic_posts_url, notice: 'Post deleted.'}
end
#DOES NOT work: redirect_to root_url([:blog, @topic, @post]), notice: 'Post deleted.'
end
private
def fetch_post
@post = @topic.posts.find(params[:id])
end
def fetch_topic
@topic = Topic.find(params[:topic_id])
end
这是我的模板:
<%= link_to 'Destroy', blog_topic_post_path(@topic, @post), method: :destroy, confirm: 'You Sure About This?' %>