3
votes

I have an application where resources are saved in the database by a normal ID. However, to prevent database enumeration, the IDs that are shown to the users in paths and links are UUID-like. (An example: http://example.com/payment/yta6Bo34s)

To create normal, id-based links in Phoenix+Ecto, one can just do:

link to: payment_path(@conn, :show, payment)

However, this implicitly uses the .id field of the %Payment{} struct that is passed here.

When using an UUID, one has to do

link to: payment_path(@conn, :show, payment.uuid)

or, in the case of my application where the UUIDs are saved as bigints in the database, but shown as base36-strings:

link to: payment_path(@conn, :show, UUID.encode(payment.uuid))

Is there a way to streamline this process? For instance, is there a protocol that can be implemented for structs passed to the Phoenix path helpers, so they'll automatically use a custom procedure to generate show/edit/update/delete paths?

1

1 Answers

3
votes

I think you could implement to_param as described here or the derive form as described here.