redirect_to in action摧毁ruby on rails

时间:2014-12-31 01:57:17

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2

我在db / config

中有以下资源
resources :posts do

      resources :comments

    end

我的控制器内部有以下操作,名为Comment

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

当我删除评论栏重定向到@post

的网址时
example /posts/1

但我不想重定向到该网址,而是

posts/1/comments 

1 个答案:

答案 0 :(得分:3)

使用

redirect_to post_comments_path(@post)

在命令行中,如果您执行rake routes,它会显示:

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

post_comments前缀可让您知道有两个可用的帮助:post_comments_pathpost_comments_url,用于生成关联的路径或网址。