Thank you, but these are only good for simple routing requirements… At one point, you have to use the slug as your primary id. I don’t want to use the slug as my primary id because you can never update the title.
In the documentation, it says that it takes *slug as a param, but the router doesn’t take it as a param… (I inject it from index.html as a link to show.html)
There is not much code related to this aspect…
There is classic get "/:id/*slug“, PostController, :show with *slug added.
I was looking if I could inject the post.slug field from the index like post_path(@conn, :show, post, slug: post.slug
Error is: no function clause matching in URI.encode/2
Attempted function clauses (showing 1 out of 1) def encode(string, predicate) when is_binary(string) and is_function(predicate, 1)
I tried that, didn’t work for me… There are attached posts to main post. They need to get and write add_id, etc… It was getting to a point I didn’t like.
There is something wrong with (*) glob path… I don’t know how, but it dominates the session and you have to move every line in the router above this glob path line… It’s not usable at this state.
Then you can change to using /:id/:title and post_path(@conn, :show, post, post.slug).
But if there is a chance that the :id can be sent without the :title you’ll need both routes:
get "/:id", PostController, :show
get "/:id/:title", PostController, :show
And in the controller you may want to create two function heads and pattern match:
def show(conn, %{"id" => id}) do
# do something
end
def show(conn, %{"id" => id, "title" => title}) do
# do something
end
But it really depends on if you need the value of :title or not and what you’re doing inside show/2. You may want to just do show(conn, params) and get the values via params["id"] or params["title"] etc.