Using a date as a param in path helpers

Is it possible to adapt path helpers to take a %Date{} struct as a param?

path = proj_apprvl_path(conn, :index, project, week_end_date)

ATM we are having to run:

week_end_date =  Date.to_string(week_end_date)
path = proj_apprvl_path(conn, :index, project, week_end_date)

The route is defined:

scope "/projects/:proj_id", as: :proj do
  get("/timecards/:week_end_date/approval", TimecardApprovalController, :index, as: "apprvl")

I can see from the Phoenix.Param docs that we can define a specific param for a given struct but I can’t see how to adapt for me because we are not passing a custom struct but the elixir %Date{} one.

Unless I were to create a specific %WeekEndDate{} struct, which I don’t think I want to.

Thanks a lot for any assistance.


What stops you from implementing phoenix param for the date struct?

defimpl Phoenix.Param, for: Date do
  def to_param(date) do

Would it work? (I haven’t tried)

1 Like

To be honest I didn’t really understand if/how that would work.

Can I ask where I’d define that?

I think you can define it anywhere in the lib/ directory (so that it gets compiled).

Great, will try and update. Cheers :+1:

That seemed to do the trick. Thanks @idi527 :slight_smile: