Asimov
Where the :id in a route comes from?
Hi all,
I am following along the excellent Phoenix in Action book from @geo. In page 59, while he takes us through the life cycle of a request through Phoenix, he says:
from our list of routes, /posts/:id is the path that gets you to this function. The “:id” portion of that route specifies that whatever you put into that position will be passed as the id in the params map.
But when I look at scopes in the router.ex (or the endpoint.ex file before that), I do not see the :id portion anywhere.
defmodule BlogWeb.Router do
# ...
scope "/", BlogWeb do
pipe_through :browser
get "/", PageController, :index
resources "/posts", PostController do
resources "/comments", CommentController, only: [:create]
end
end
scope "/api", BlogWeb.API do
pipe_through :api
resources "/posts", PostController, only: [:index, :show]
end
end
Is it supposed to be implied anywhere? How about if I do not what the id but another parameter. Or am I not understanding the philosophy of it?
Thanks very much!
Most Liked
LostKobrakai
wolfiton
To be able to access the resources route you need to have:
- a migration
- a schema
- a controller
- a view
- a template.
So you use a mix generator to do that for you but those things need to exist to be able to use the resource route.
kokolegorille
The following command might be useful if You want to inspect your routes.
$ mix phx.routes
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









