Today I Learned

hashrocket A Hashrocket project

Change resource param in Phoenix routes

Phoenix, like Rails, gives you the option to specify a resource in your routes in order to generate the typical CRUD actions we deal with in a controller. The dynamic segment which identifies the resource is :id by default.

resources "orders", OrderController

The update route would look like this if we run mix phoenix.routes.

order_path  PATCH  /orders/:id    MyApp.OrderController :update

If our table doesn't use "id" as a primary key, or we want to change the field we use to find our resource, we can specify it as an option in our route:

resources "orders", OrderController, param: "some_field"

Now our route is updated:

order_path  PATCH  /orders/:some_field    MyApp.OrderController :update

See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.