有効なルートを持つオブジェクトを破棄できません。ブラウザは を返します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?' %>