travisf

travisf

Routing with a custom slug

I have a record where users can provide an optional slug which is a unique_index to provide a friendly name for the URL, how can I go about using this slug if it’s defined otherwise falling back to the resource’s UUID?

live "/records/:id", RecordsLive.Show

IE: live/records/slug-is-defined or live/records//77?

Marked As Solved

ibarch

ibarch

Ecto will likely raise an error if you try to cast a plain string as UUID so I suggest to determine the datatype on the application level:

live "/records/:opaque_id", RecordsLive.Show

def get_record(opaque_id) do
  Record
  |> where(^opaque_id_to_query(opaque_id))
  |> Repo.one()
end

defp opaque_id_to_query(opaque_id) do
  cond do
    match?({_, ""}, Integer.parse(opaque_id)) -> [id: opaque_id]
    match?({:ok, _}, Ecto.UUID.cast(opaque_id)) -> [uuid: opaque_id]
    true -> [slug: opaque_id]
  end
end

Also Liked

csadewa

csadewa

Not necessarily, using or on different field with index usually result in query optimizer combine these two different index in bitwise or operation. Usually it’s not faster to do lookup one then the other because it cause two DB request network latency.

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Hey @travisf you’d simply do something like instead of:

Thing |> where([t], t.id == ^params.id) |> Repo.one

you

Thing |> where([t], t.id == ^params.id or t.slug == ^params.id) |> Repo.one
sodapopcan

sodapopcan

Have a look at Phoenix.Param. You could do something like:

defimpl Phoenix.Param, for: YourApp.SomeContext.SomeSchema do
  def to_param(schema) do
    Map.get(schema, :slug, schema.id)
  end
end

To offer some unsolicited advice, I think it’ll be far less of a headache in the future if you make slugs non-nullable fields and auto-generate them based off of some other field as to not force users to specify one. Take it or leave it, of course!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement