简体   繁体   中英

Rails3 - Routing Custom Controller Actions

In my Rails 3 application, I want to be able to route to the following paths:

  • /admin/automobiles/get_makes_for_year
  • /admin/automobiles/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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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