如何将许多类似的方法和视图合并为一个

时间:2016-05-27 04:19:59

标签: ruby-on-rails-4 refactoring

我想将类似的方法和视图合并为一个,但仍保留url名称,如下所示:

Home/recommends/categories/shopping
Home/recommends/categories/nightview
Home/recommends/categories/food
Home/recommends/categories/area

我不想在网址中使用"?something=xyz"之类的参数。

routes.rb

resources :recommends, only: :index do
  collection do
    resources :categories, only: :show, controller: 'recommends' do
      collection do
        get :food
        get :area
        get :shopping
        get :nightview
      end
    end
  end
end

在控制器中:

def food
  set_paginator
  @recommends = UserRecommend.where(category: "food").order('created_at desc').offset(@offset).limit(@limit).all
  @number_of_recommends = UserRecommend.where(category: "food").count
end

def area
  set_paginator
  @recommends = UserRecommend.where(category: "area").order('created_at desc').offset(@offset).limit(@limit).all
  @number_of_recommends = UserRecommend.where(category: "area").count
end

...

在观点中我有:

food.html.slim
area.html.slim
shopping.slim
nightview.slim

使用相同的代码,h1中只有不同的名称:

h1
  | Shopping ( or Area or Food... )
  = " (#{@number_of_recommends})"

= render partial: "layouts/paginator",
  locals: { total_items: @number_of_recommends, per_page: @limit, current_page: @page }

= render partial: "table", locals: { recommends: @recommends }

有人可以帮我重构这段代码吗?

1 个答案:

答案 0 :(得分:3)

您可以(并且应该)拥有单个路线,单个动作和单个视图。关键是将URL的可变部分变为实际变量。您可以使用dynamic segments执行此操作。

首先,单一路线。如果您实际上没有生成多个RESTful操作,则无需使用resources

get "/recommends/categories/:category" => "categories#show"

您可以添加:category细分受众群的标准:

get "/recommends/categories/:category" => "categories#show", category: /food|area|shopping|nightview/

接下来,一个动作:

class CategoriesController < ApplicationController
  before_action :set_paginator

  def show
    # params[:category] is "food"/"area"/etc
    categories = UserRecommend.where(category: params[:category]).order('created_at desc')
    @recommends = categories.offset(@offset).limit(@limit)
    @number_of_recommends = categories.count
  end
end

最后,单一观点:

# app/views/categories/show.slim
h1
  = params[:category].capitalize
  = " (#{@number_of_recommends})"

= render partial: "layouts/paginator",
  locals: { total_items: @number_of_recommends, per_page: @limit, current_page: @page }

= render partial: "table", locals: { recommends: @recommends }

我认为最好使用本地化将params[:category]转换为标题,这样可以提供更多控制权,而不是依赖于网段的简单大写:

# app/views/categories/show.slim
h1
  = t params[:category]

并且:

# config/locals/en.yml
en:
  categories:
    show:
      food: 'Food'
      area: 'Area'
      nightview: 'Night View'