RailsBestPractices - 过度使用路由自定义

时间:2015-04-03 16:05:42

标签: ruby-on-rails ruby-on-rails-4

在所有之前,这不是关于最佳做法的问题,我知道你不喜欢它。在不同之前阅读我的问题!

我正在使用gem rails_best_practices来检测我项目中的一些错误做法。最近,我有几个“过度使用路线自定义”(http://rails-bestpractices.com/posts/2010/07/22/overuse-route-customizations/)。

我的路线如下:

resources :projects do
  collection do
    get :featured
    get :computed
    get :static
    get :blocked
  end

链接到这些路由的操作只是具有状态范围的特定index,如下所示:

def featured
  @projects = Project.featured.paginate(...)
end

我使用这些路线来获得像/projects/featured而不是/projects/?state=featured

这样漂亮的网址

我的问题是如何更改这些当前路由以与漂亮的网址具有相同的行为?

1 个答案:

答案 0 :(得分:0)

在故事资源

之前添加此内容
get '/projects/:state', to: 'projects#index', constraints: { state: /featured|computed|static|blocked/ }

或者在里面:

collection do
  get '/projects/:state', to: 'projects#index', constraints: { state: /featured|computed|static|blocked/ }
end