2
votes

In my Phoenix Application, I'd like to have URIs like http://localhost:4000/2016/07/24/, which are generated by a date field in a struct. I've created a Phoenix.Param impl for my struct:

defimpl Phoenix.Param, for: Item do
  def to_param(item) do
  {{year, month, day}, _time} = item.date
  "#{year}/#{month}/#{day}/"
end

end

Which works as expected, but unfortunately (but understandably) the path helper is escaping that string, so the URI generated by item_path(@conn, :show, item) is http://localhost:4000/2016%2F07%2F24%2F.

Is there any way to have to path helper accept the path as-is without escaping it? I'm digging through the Phoenix.Router.Helpers module, but nothing is jumping out at me.

Edit: I'll add that my current workaround for this is just to define my route as /:year/:month/:day instead of just /:date, and handle it in my controller's show function appropriately, but I'd really prefer the /:date route approach.

1
Given how ambiguous date formatting can be, I would appreciate the clarity of /:year/:month/:day as a reader of your code. - Martin Svalin

1 Answers

2
votes

The solution is to use &URI.decode/1

@conn
|> item_path(:show, item)
|> URI.decode()