bodyguard icon indicating copy to clipboard operation
bodyguard copied to clipboard

Readme example confusion

Open tjchambers opened this issue 3 years ago • 1 comments

In the readme in the Plugs section I see the following code:


  defp get_post(conn, _) do
    assign(conn, :post, MyApp.Posts.get_post!(conn.params["id"]))
  end

  # Helper for the Authorize plug
  def extract_post(conn), do: conn.assigns.posts

My confusion is how does posts become an element of assigns?

tjchambers avatar Jan 04 '23 20:01 tjchambers

at the top there is a plug

  # Fetch the post and put into conn assigns
  plug :get_post when action in [:show]

so before executing :show, first this plug is run which assigns the post to the connection.

a-c-sreedhar-reddy avatar Feb 13 '23 07:02 a-c-sreedhar-reddy