patternrubyrailsMinor
Clean up Rails Routes
Viewed 0 times
railsroutesclean
Problem
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 => :deleteSolution
Yes, use
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
My way also uses the correct verb (PUT) for updates.
Your
My
namespace and resources:namespace :oauth do
resources :apps, controller: "applications", as: :applications
endThis 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#destroyMy
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#destroyCode Snippets
namespace :oauth do
resources :apps, controller: "applications", as: :applications
endoauth_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#destroyoauth_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#destroyContext
StackExchange Code Review Q#26531, answer score: 6
Revisions (0)
No revisions yet.