Ruby on rails 3 Rails嵌套资源和路由-如何分解控制器?

Ruby on rails 3 Rails嵌套资源和路由-如何分解控制器?,ruby-on-rails-3,model-view-controller,Ruby On Rails 3,Model View Controller,我有以下型号: 职位 标签 TaggedPost(Post和Tag通过has_many:through从中派生其关联) 我有以下routes.rb文件: resources :tags resources :posts do resources :tags end 因此,当我导航到,比如说,/posts/4/tags,这将使用参数数组中设置的post\u id值为标记控制器执行索引操作。酷 但我的问题是,既然我正在访问POST下的嵌套标记资源,我还应该点击标记控制器吗?或者我应该设置

我有以下型号:

  • 职位
  • 标签
  • TaggedPost(Post和Tag通过has_many:through从中派生其关联)
我有以下
routes.rb
文件:

resources :tags

resources :posts do
  resources :tags
end
因此,当我导航到,比如说,
/posts/4/tags
,这将使用参数数组中设置的
post\u id
值为标记控制器执行索引操作。酷

但我的问题是,既然我正在访问POST下的嵌套标记资源,我还应该点击标记控制器吗?或者我应该设置一些其他的控制器来处理标签的嵌套性质?否则,我必须在Tags控制器中构建额外的逻辑。这当然可以做到,但这是处理嵌套路由和资源的常用方法吗?我在标签控制器的索引操作中的代码如下所示:

TagsController.rb

def index
  if params[:post_id] && @post = Post.find_by_id(params[:post_id])
    @tags = Post.find_by_id(params[:post_id]).tags
  else
    @tags = Tag.order(:name)
  end
  respond_to do |format|
    format.html
    format.json {render json: @tags.tokens(params[:q]) }
  end
end
我可以看到这个控制器中的代码越来越大,因为我计划将许多额外的资源与标记资源相关联。关于如何打破这种局面的想法

问题摘要:

  • 如果资源是嵌套的,那么嵌套的资源是否应该经过表示资源嵌套性质的不同控制器?这与我在提供的代码示例中使用的普通控制器相反
  • 如果是,这些控制器应该如何命名和设置

  • 如果您需要更多信息,请告诉我。

    使用嵌套资源所做的一切就是更改路由URL。您需要做的唯一一件事是确保将正确的id(在您的案例帖子中)传递给标记控制器。最常见的错误是找不到***ID

    如果您不将配置文件路由嵌套到用户路由中,它将如下所示

    domain.com/user/1

    domain.com/profile/2

    当你筑巢的路线,它将是

    domain.com/user/1/profile/2

    这就是它所做的一切,没有别的您不需要额外的控制器。进行嵌套路由只是为了外观。允许您的用户遵循关联。嵌套路由最重要的一点是确保链接指向正确的路径

    不嵌套时:它将是

     user_path
    

    当它嵌套时,您需要使用

    user_profile_path
    
    rake routes
    是您的朋友,您可以了解路由是如何变化的


    希望有帮助。

    我认为最好的解决方案是拆分控制器:

        resources :tags
    
        resources :posts do
          resources :tags, controller: 'post_tags'
        end
    
    然后你有3个控制器。也可以选择继承 从TagsController中选择PostTagsController以执行以下操作:

        class PostTagsController < TagsController
            def index
                @tags = Post.find(params[:post_id]).tags
                super
            end
        end
    
    class PostTagsController
    如果差异仅在于检索标记,则可以:

        class TagsController < ApplicationController
            def tags
                Tag.all
            end
    
            def tag
                tags.find params[:id]
            end
    
            def index
                @tags = tags
                # ...
            end
            # ...
        end
    
        class PostTagsController < TagsController
            def tags
                Product.find(params[:product_id]).tags
            end
        end
    
    class标记控制器

    使用该方法并简单地覆盖继承控制器中的标记;)

    这实际上回答了我问题的核心。。。我想,除此之外,物流还取决于我的强迫性组织需求。为了你未来的同事(以及你未来的自我!),请阅读@lazel-answer!我将花费一些辛苦赚来的点数来投票。请,请,添加嵌套控制器。这个答案对任何即将到来的同事IMHO来说都更清晰、更容易理解。我知道Rails完全是干的,但在这种情况下,OP说,
    我计划将许多额外的资源与标记资源相关联
    ,因此允许区分每个关联可能会有很大的帮助…在控制器选项中,现在我们应该改为使用
    post_标记
    。请参阅以了解正确的命名约定,我认为在本例中应该是
    controller:'PostTags'
    ?我知道:资源:品牌,controller:'users/brands'。。。然后是用户::BrandsController。依我看,这与轨道设计是一致的。我不想在树中创建单独的user/dir,所以我使用现有用户/
        class TagsController < ApplicationController
            def tags
                Tag.all
            end
    
            def tag
                tags.find params[:id]
            end
    
            def index
                @tags = tags
                # ...
            end
            # ...
        end
    
        class PostTagsController < TagsController
            def tags
                Product.find(params[:product_id]).tags
            end
        end