Member and collection routes in phoenix

In Rails, I can write:

  resources :users do
    member do
      get :friends
    end
    collection do
      post :invite
    end
  end

To get the following routes:

get /users
get /users/:id 
# and so on for resource
get /users/:id/friends
post /users/invite 

The action invite relates to user but not to one user in particular, hence the absence of the :id parameter.

In phoenix, I have succeeded in getting the member action (though the syntax is not super DRY):

    resources("/users", MyApp.UserController do
      get("/friends", MyApp.UserController, :friends, as: :friends) # /users/:id/friends
    end

But to get the collection action, do I have to write:

post('/users/invite', MyApp.UserController, :invite, as: :invite)

outside the /users scope, or can I achieve this somehow?

Probably collection should be outside (no member, nor collection like in Rails). See this (old) issue.

1 Like