【发布时间】:2013-08-17 03:45:18
【问题描述】:
我在 Rails 3 应用程序中使用以下路由配置。
# config/routes.rb
MyApp::Application.routes.draw do
resources :products do
get 'statistics', on: :collection, controller: "statistics", action: "index"
end
end
StatisticController 有两个简单的方法:
# app/controllers/statistics_controller.rb
class StatisticsController < ApplicationController
def index
@statistics = Statistic.chronologic
render json: @statistics
end
def latest
@statistic = Statistic.latest
render json: @statistic
end
end
这会生成由StatisticsController 成功处理的URL /products/statistics。
如何定义指向以下 URL 的路由:/products/statistics/latest?
可选:我尝试将工作定义放入 concern,但失败并显示错误消息:
undefined method 'concern' for #<ActionDispatch::Routing::Mapper ...
【问题讨论】:
标签: ruby-on-rails collections separation-of-concerns nested-resources nested-routes