daniel barrera
daniel barrera

Reputation: 367

redirect_to in action destroy ruby on rails

I have the following resources in db/config

resources :posts do

      resources :comments

    end

I have the following action destroy inside of my controller called Comment

  def destroy
  @post = Post.find(params[:post_id])
  @comment = @post.comments.find(params[:id])
  @comment.destroy
  redirect_to post_path(@post)
 end

When I eliminate the comment rails redirect to the url of @post

example /posts/1

But I don't want to redirect to that url but to

posts/1/comments 

Upvotes: 0

Views: 217

Answers (1)

Matt
Matt

Reputation: 20796

Use

redirect_to post_comments_path(@post)

From the command line, if you do rake routes it will show you:

       Prefix Verb    URI Pattern                                                                       Controller#Action
post_comments GET     /posts/:post_id/comments(.:format)                                                comments#index

The post_comments prefix lets you know that you have the two helpers available: post_comments_path and post_comments_url for generating the associated path or url.

Upvotes: 3

Related Questions