新手问题 Post 与 Comment 关联删除的时候,

zhangyanan · 2013年11月15日 · 最后由 Rei 回复于 2013年11月15日 · 1931 次阅读
def destroy
   @post = Post.find(params[:post_id])
   @comment = @post.comments.find(params[:id])
   @comment.destroy
   redirect_to post_path(@post)
 end

这里的,params[:post_id] 是怎么取到的?post 对象不是 id 字段吗?为什么这样可以取到值?

http://guides.ruby.tw/rails3/getting_started.html ,搜索 params[:post_id] 即可找到。

因为它这里用了嵌套路由

resources :posts do
  resources :comments
end

Url 模式是:

/posts/:post_id/comments/:id

你可以用

rake routes

来查看现在的路由规则。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号