Search code examples
ruby-on-railsactiverecordcontrolleractioncontroller

Route present, but can't DESTROY object, why?


I am unable to destroy an object with a valid route. Browser returns No route matches [POST] "/blog/topics/3/posts/1". However I can perform all other actions on the same resource. How should my controller, template look given I can create and destroy the object from the console?

Save time-these routes don't work either under my current config:

Here is my controller:

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

Here is my template:

   <%= link_to 'Destroy', blog_topic_post_path(@topic, @post), method: :destroy, confirm: 'You Sure About This?' %>

Solution

  • It should be method: :delete.