Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Url to accept a string instead id in phoenix framework (elixir)

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.

like image 371
Robin Solanki Avatar asked Jan 02 '16 20:01

Robin Solanki


1 Answers

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).

like image 134
José Valim Avatar answered Oct 02 '22 05:10

José Valim