Dorian
Dorian

Reputation: 23969

Improve my routes join/disjoin

I have a lot of join/disjoin routes, how could I improve the way I wrote this ? Or could I make it RESTful ?

  match 'events/:id/join' => 'events#join', :as => 'join_event'
  match 'events/:id/disjoin' => 'events#disjoin', :as => 'disjoin_event'

  match 'assos/:id/join' => 'assos#join', :as => 'join_asso'
  match 'assos/:id/disjoin' => 'assos#disjoin', :as => 'disjoin_asso'

  match 'projects/:id/join' => 'projects#join', :as => 'join_project'
  match 'projects/:id/disjoin' => 'projects#disjoin', :as => 'disjoin_project'

  match 'roles/:id/join' => 'roles#join', :as => 'join_role'
  match 'roles/:id/disjoin/:user_id' => 'roles#disjoin', :as => 'disjoin_role'

  match 'groups/:id/join' => 'groups#join', :as => 'join_group'
  match 'groups/:id/disjoin/:user_id' => 'groups#disjoin', :as => 'disjoin_group'

Upvotes: 0

Views: 66

Answers (1)

Ryan Brunner
Ryan Brunner

Reputation: 14851

The reason that this route doesn't seem RESTful is because you're organizing things in terms of actions, rather than resources. When you're setting up RESTful routes for an application, think about what resources you're operating on, and then limit yourself to the core actions defined by HTTP verbs for those actions.

To take one example, one way of expressing "join a group" might be to have a membership resource as a nested route on that group. Then you can perform different operations on that resource, such as POST for creating a membership (joining a group), and DELETE for removing a membership ("disjoining" a group). Your routes can be set up like this:

resources :events do
  resources :members, :only => [:create, :destroy]
end

Then you could make a call to join a group by POSTing to /events/1/members, and disjoin by sending DELETE to /events/1/members/(membership_id)

Upvotes: 1

Related Questions