3
\$\begingroup\$

Is there any way I can DRY up this Rails 4 routes.rb code?

  match '/oauth/apps/' => 'oauth/applications#index', :as => :oauth_applications, :via => :get
  match '/oauth/apps/new/' => 'oauth/applications#new', :as => :new_oauth_application, :via => :get
  match '/oauth/apps/new/' => 'oauth/applications#create', :via => :post
  match '/oauth/apps/:id/' => 'oauth/applications#show', :as => :oauth_application, :via => :get
  match '/oauth/apps/:id/edit/' => 'oauth/applications#edit', :as => :edit_oauth_application, :via => :get
  match '/oauth/apps/:id/edit/' => 'oauth/applications#update', :via => :post
  match '/oauth/apps/:id/destroy/' => 'oauth/applications#destroy', :as => :destroy_oauth_application, :via => :delete
\$\endgroup\$
6
\$\begingroup\$

Yes, use namespace and resources:

namespace :oauth do
  resources :apps, controller: "applications", as: :applications
end

This is not 100% identical (the names of some of your routes have changed) but it is the way you should be building your routes, and you should change the rest of your app to reflect the change.

The primary difference is your names for the create and destroy routes have gone away; these routes shouldn't have names anyways. When you want to create/destroy a model, you should be using oath_applications_path, with method: :post or method: :delete, not oath_apps_new_path/destroy_oauth_application_path.

My way also uses the correct verb (PUT) for updates.

Your rake routes:

       oauth_applications GET    /oauth/apps(.:format)             oauth/applications#index
    new_oauth_application GET    /oauth/apps/new(.:format)         oauth/applications#new
           oauth_apps_new POST   /oauth/apps/new(.:format)         oauth/applications#create
        oauth_application GET    /oauth/apps/:id(.:format)         oauth/applications#show
   edit_oauth_application GET    /oauth/apps/:id/edit(.:format)    oauth/applications#edit
                          POST   /oauth/apps/:id/edit(.:format)    oauth/applications#update
destroy_oauth_application DELETE /oauth/apps/:id/destroy(.:format) oauth/applications#destroy

My rake routes (reordered to match yours):

    oauth_applications GET    /oauth/apps(.:format)          oauth/applications#index
 new_oauth_application GET    /oauth/apps/new(.:format)      oauth/applications#new
                       POST   /oauth/apps(.:format)          oauth/applications#create
     oauth_application GET    /oauth/apps/:id(.:format)      oauth/applications#show
edit_oauth_application GET    /oauth/apps/:id/edit(.:format) oauth/applications#edit
                       PUT    /oauth/apps/:id(.:format)      oauth/applications#update
                       DELETE /oauth/apps/:id(.:format)      oauth/applications#destroy
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Wow thank you for this. I never thought of doing it this way and I guess doing things the conventional way is better then anything else. \$\endgroup\$ – ny95 May 24 '13 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.