Change which struct-key the Phoenix path-helpers use

By default the Elixir Phoenix path helpers use the :id key. For example: property_path(conn, :show, property) would turn into /properties/1. If you want to use a different key inside the property use the Phoenix.Param protocol in the schema definition.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
defmodule Property do
  use Ecto.Schema
  import Ecto.Changeset

  @derive {Phoenix.Param, key: :identifier}   # <--- THIS!
  @type t :: %__MODULE__{}

  schema "properties" do
    field :identifier, :string
    field ...
  end
end

The @derive {Phoenix.Param, key: :identifier} instructs the path helpers to use the :identifier key. Now the path above would turn into /properties/that_identifier instead.

areal photograph of mountain path splitting into two paths

Sending Sometimes Sundays

Subscribe to my newsletter for updates