Rails: Nested resources conflict, how to scope the index action depending on the called route

前端 未结 4 1278
走了就别回头了
走了就别回头了 2021-02-13 19:50

Imagine you have two defined routes:

map.resources articles
map.resources categories, :has_many => :articles

both accessible by helpers/path

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-13 20:30

    You have two choices here, depending on how much your logic and your view is tied to the scope. Let me explain further.

    The first choice is to determine the scope within your controller, as already explained by the other responses. I usually set a @scope variable to get some additional benefits in my templates.

    class Articles
    
      before_filter :determine_scope
    
      def index
        @articles = @scope.all
        # ...
      end
    
      protected
    
        def determine_scope
          @scope = if params[:category_id]
            Category.find(params[:category_id]).articles
          else
            Article
          end
        end
    
    end
    

    The reason for the @scope variable is that you might need to know the scope of your request outside the single action. Let's assume you want to display the number of records in your view. You need to know whether you are filtering by category or not. In this case, you simply need to call @scope.count or @scope.my_named_scope.count instead of repeating each time the check on params[:category_id].

    This approach works well if your views, the one with category and the one without category, are quite similar. But what happens when the listing filtered by category is completely different compared to the one without a category? This happens quite often: your category section provides some category-focused widgets while your article section some article-related widgets and filter. Also, your Article controller has some special before_filters you might want to use, but you don't have to use them when the article listing belongs to a category.

    In this case, you might want to separate the actions.

    map.resources articles
    map.resources categories, :collection => { :articles => :get }
    
    articles_path # /articles and ArticlesController#index
    category_articles_path(1) # /category/1/articles and CategoriesController#articles
    

    Now the listing filtered by category is managed by the CategoriesController and it inherits all the controller filters, layouts, settings... while the unfiltered listing is managed by the ArticlesController.

    This is usually my favorite choice because with an additional action you don't have to clutter your views and controllers with tons of conditional checks.

提交回复
热议问题