新手问题 Rails nested resources 如何判断在哪个嵌套的资源下面?

bastengao · June 19, 2014 · Last by ken replied at September 26, 2014 · 2458 hits

routes.rb

resources :posts do
  resources :comments
  resources :tags
  ...
end

例如我有上面的路由,在 comments 和 tags 的页面中如何判断他们在 posts 下面?

params[:post_id]就会告诉你他们在哪个 post 下面。这个参数是在路径里面的 /post/:post_id/comment/:id

#1 楼 @billy thanks, 你说的是个好办法。但是如何在 /posts 也能判断呢?

/posts 直接到 PostsController#index,是 posts 列表,不是具体的哪个了。

comment 和 tag 都要有个属性 post_id 查询@comment.post_id,就能找到是哪个 post 了。

You need to Sign in before reply, if you don't have an account, please Sign up first.