Fork me on GitHub
#reitit
<
2020-12-17
>
oly10:12:46

Hi I would like to know how I can handle an optional param like below

(reitit/router
   [["/editor" :editor]
    ["/editor/:slug" :editor]])

oly10:12:19

I guess I could have :editor and :editor-slug but is there a way to say slug does not have to exist

oly10:12:00

this is using the frontend routing, but i imagine the syntax is the same for both

Steven Deobald16:12:31

@oliver.marks I think your options are {:conflicts nil} for all routes or {:conflicting true} for a specific route.

oly16:12:52

@steven427 thanks I thought would be a better way than that 😕

oly16:12:29

although :editor :editor-slug did work and having them both call the same view function

oly16:12:11

just felt like there should be a better way would explain why I could not find it in the docs as well

Steven Deobald17:12:41

@oliver.marks Oh, sorry. I missed half your question. Yeah, I was actually just doing something similar (sort of the inverse of your problem with :editor) yesterday because I want to replicate Rails’ resource-oriented named-route lookups. I ended up hacking up an :alias feature for the path lookup: https://github.com/pariyatti/kosa/blob/e59bf595663381ab24f31fbbaf1e2f8337bf62a0/src/kosa_crux/routes.clj#L58 https://github.com/pariyatti/kosa/blob/e59bf595663381ab24f31fbbaf1e2f8337bf62a0/src/kosa_crux/views.clj#L15 That way I can do something like (path-for request :show id) and (path-for request :destroy id) and they both return the same path: "/images/:id"