richjdsmith
I am currently going through the Programming Phoenix 1.4 book and so far am really enjoying it. I didn’t think I’d appreciate a lack of ‘magic’ as much as I do.
Anyhow, I’m a bit stumped on how one thing works and would appreciate someone pointing me in the direction of where in the Elixir doc’s I could see what’s going on.
In a controller, I (was instructed to) wrote(ite) the following code:
def action(conn, _) do
args = [conn, conn.params, conn.assigns.current_user]
apply(__MODULE__, action_name(conn), args)
end
and then I was able to write:
def new(conn, _params, current_user) do
changeset = Multimedia.change_video(current_user, %Video{})
render(conn, "new.html", changeset: changeset)
end
This code pulls the current_user id from the conn and let’s me access it as an argument in any other function within that controller module. What I don’t understand, is how this modification to other functions is working, and why/how it doesn’t force the other controller actions to have it as an argument.
What I mean is, why can I still write: def index(conn, _params) do and have no problems, but elsewhere, I’m also able to write def new(conn, _params, current_user) do all because of that one function.
Any help and explanation on this is appreciated!
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
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
- #blog-post
- #phoenix_html
- #ai
- #iex
- #graphql
- #elixirconf-us
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
The source code actually updates all actions to 3 parameters.
For some more background
https://medium.com/@lostkobrakai/alternative-parameters-for-phoenix-controller-actions-edd2b8a4362a
This works:
This fails:
with
on the
indexaction when rendering the page - which is the behaviour I would have expected if you leftdef index(conn, _params) doin place for that controller after adding the third parameter in theactionfunction.after
everything works again.
Maartz
So if I guess it right, it’s related to function clauses ? Right ?
peerreynders
I was simply stating that I couldn’t find any evidence to support this observation:
My observation was that customizing the arguments by overriding
action/2forces an update of the parameter list of every action function within that controller because they all have the same arity, argument order and types.Any controller includes something like this:
which injects code
then
injects even more code phoenix/lib/phoenix/controller.ex at 19b0b01e2fc751901e06fcd74db1fc5b39672d26 · phoenixframework/phoenix · GitHub
then
injects among other things this code phoenix/lib/phoenix/controller/pipeline.ex at 19b0b01e2fc751901e06fcd74db1fc5b39672d26 · phoenixframework/phoenix · GitHub
init/1andcall/2define a module plug that adds theconn.private.phoenix_controllerandconn.private.phoenix_controllervalues to thePlug.Connstruct.action/2is the controller function normally used to extractconn.private.phoenix_actionandconn.paramsto call the appropriate controller action function withBut because
action/2is listed asdefoverridableyou are free to provide your own implementation ofaction/2inside your controller module to invoke the action functions whichever way you wish.Now you may be wondering
where is
actioncoming from?As far as I can tell it originates from
router.ex(Phoenix.Router.scope/2):Phoenix.Router.get/4:If
GET /api/v1/pages/:idthenAPI.V1.PageController→plug:show→plug_optsSee also:
Maartz
Wow, thanks ! That’s very helpful to understand what’s going on behind the scenes. No magic.