Rails3 - 路由自定义控制器操作

时间:2011-01-10 23:30:32

标签: ruby-on-rails ruby-on-rails-3

在我的Rails 3应用程序中,我希望能够路由到以下路径:

  • /管理/汽车/ get_makes_for_year
  • /管理/汽车/ get_models_for_make_and_year

我有以下路线可以完成工作。

Moonshine::Application.routes.draw do
  # Administration
  match 'admin/automobiles/get_makes_for_year' => 'admin/automobiles#get_makes_for_year'
  match 'admin/automobiles/get_models_for_make_and_year' => 'admin/automobiles#get_models_for_make_and_year'
  namespace "admin" do
    resources :automobiles
  end
end

但是,以这种方式映射自定义路由不会感觉正确。有没有更好的方法来实现自定义控制器操作的路由?我在想有一种方法可以使用:controller, :action通配符,或者像下面这样的东西。

Moonshine::Application.routes.draw do
  # Administration
  namespace "admin" do
    resources :automobiles do
      get :get_makes_for_year
      get :get_models_for_make_and_year
    end
  end
end

1 个答案:

答案 0 :(得分:4)

你可以这样做:

Moonshine::Application.routes.draw do
  # Administration
  namespace "admin" do
    resources :automobiles do
      get :get_makes_for_year, :on => :collection
      get :get_models_for_make_and_year, :on => :collection
    end
  end
end