I am trying to implement permalinks in phoenix app.
The goal is to change localhost:4000/products/1 to localhost:4000/products/productname
I tried following Ryan Bates episode on permalinks implementation in rails but wasn't able to find a to_param function for models in phoenix.
Please help.
In addition to Wobbley's response, to_param
in Phoenix is implemented with protocols. For example, here is how you could change how the URLs for products are generated:
defimpl Phoenix.Param, for: MyApp.Product do
def to_param(%{name: name}) do
"#{name}"
end
end
A more complex example is also shown on Programming Phoenix (disclaimer: I am one of the authors).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With