简体   繁体   中英

More elegant Rails routing then a giant list

I have an administration controller for my Rails application, but its getting large and unwieldly. Here is a sample:

get '/administration/events', :controller => :administration, :action => :event_index
get '/administration/events/new', :controller => :administration, :action => :event_new
post '/administration/events/create', :controller => :administration, :action => :event_create
get '/administration/events/:id/edit', :controller => :administration, :action => :event_edit
post '/administration/events/:id/update', :controller => :administration, :action => :event_update
delete '/administration/events/:id/delete', :controller => :administration, :action => :event_delete

It works, but is there a better way of doing it? (my actual routes for the controller are 3 or 4 times this number of entries.

You should use namespacing instead:

namespace :administration do
  resources :events
end

You seem to have build one giant controller with a lot of actions handling a set of resources. The example you show: events . This should be in a separate Administration::EventsController .

Your url's seem REST-style, now you should match that to your app-structure.

Your directory structure should look like this

/app
  /controllers
    /administration
      events_controller.rb
      ..

And then your routing will become

namespace :administration do
  resources :events
  # ... other resources ...
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