【问题标题】:Nested resources in view page. (3 levels)视图页面中的嵌套资源。 (3级)
【发布时间】:2014-03-15 19:58:02
【问题描述】:

相信大家对普通帖子和待展示的cmets模型都很熟悉了。现在想象这些关系成立。我想将replies 添加到我的 cmets 中,所以我们的路由如下

resources :posts do
    resources :comments do
        resources :replies do
    end
end

我在我的观点中尝试了许多不同的方法来实现这一点,但我没有运气!当我的实际控制器是post 并且我想访问replies 时,它只是不起作用。我尝试搜索它,但没有找到合适的名称。是否有任何资源提供有关如何实现此系统的信息或有关如何使其在 n 级而不是 2 级上工作的代码 sn-p?

【问题讨论】:

    标签: sql ruby-on-rails ruby associations nested-forms


    【解决方案1】:

    http://nithinbekal.com/posts/rails-shallow-nesting/

    您可以改用浅层嵌套。

    resources :posts, shallow: true do
        resources :comments do
            resources :replies do
        end
    end
    

    这样你只有在需要创建嵌套对象[new and create action]或查看所有相关对象[index view]时才有嵌套路由。看看下面的前三行,看看我的意思。然后你有不需要知道关系的资源的正常路由。

         comment_replies GET    /comments/:comment_id/replies(.:format)         replies#index
                         POST   /comments/:comment_id/replies(.:format)         replies#create
       new_comment_reply GET    /comments/:comment_id/replies/new(.:format)     replies#new
              edit_reply GET    /replies/:id/edit(.:format)                     replies#edit
                   reply GET    /replies/:id(.:format)                          replies#show
                         PATCH  /replies/:id(.:format)                          replies#update
                         PUT    /replies/:id(.:format)                          replies#update
                         DELETE /replies/:id(.:format)                          replies#destroy
           post_comments GET    /posts/:post_id/comments(.:format)              comments#index
                         POST   /posts/:post_id/comments(.:format)              comments#create
        new_post_comment GET    /posts/:post_id/comments/new(.:format)          comments#new
            edit_comment GET    /comments/:id/edit(.:format)                    comments#edit
                 comment GET    /comments/:id(.:format)                         comments#show
                         PATCH  /comments/:id(.:format)                         comments#update
                         PUT    /comments/:id(.:format)                         comments#update
                         DELETE /comments/:id(.:format)                         comments#destroy
                   posts GET    /posts(.:format)                                posts#index
                         POST   /posts(.:format)                                posts#create
                new_post GET    /posts/new(.:format)                            posts#new
               edit_post GET    /posts/:id/edit(.:format)                       posts#edit
                    post GET    /posts/:id(.:format)                            posts#show
                         PATCH  /posts/:id(.:format)                            posts#update
                         PUT    /posts/:id(.:format)                            posts#update
                         DELETE /posts/:id(.:format) 
    

    【讨论】:

    • 谢谢。我确实使用了浅嵌套并尝试循环遍历我的每个元素,而不是使用 rails 助手!
    【解决方案2】:

    经验法则:

    资源的嵌套深度不得超过 1 级。

    我建议您阅读 Jamis Buck 的 this article。您可能想要执行以下操作:

    resources :posts do
      resources :comments
    end
    
    resources :comments
      resources :replies
    end
    

    【讨论】:

    • 那我怎样才能实现这样的关联而不在回复中存储 post_id 呢?
    • 我已经更新了帖子。关联很简单:cmets 有一个 post_id 并回复一个 comment_id。
    猜你喜欢
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多