Brian Wigginton
Brian Wigginton

Reputation: 2652

Rails3 - Routing Custom Controller Actions

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

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

Upvotes: 0

Views: 1605

Answers (1)

Costa Walcott
Costa Walcott

Reputation: 887

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

Upvotes: 4

Related Questions