简体   繁体   English

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

[英]Rails3 - Routing Custom Controller Actions

In my Rails 3 application, I want to be able to route to the following paths: 在我的Rails 3应用程序中,我希望能够路由到以下路径:

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

I have the following routes in place which gets the job done. 我有以下路线可以完成工作。

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

However, mapping custom routes in this way doesn't feel right. 但是,以这种方式映射自定义路由感觉不对。 Is there a better way to implement routes to custom controller actions? 有没有更好的方法来实现到定制控制器动作的路由? I was thinking there would be a way using the :controller, :action wildcards or alternatively something like the following. 我在想会有一种方法使用: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

You can do: 你可以做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM