Get parent id in child controller

I´m rendering a new form (document - uploads) inside a parent show form (contact). What would be the best way to get the parent id inside the child controller.

Referer, conn assign, … Any ideas / best practise?

thank you.

1 Like

I just pass them in to the render method so it will appear in the assigns directly.


If you’re rendering a form, ideally you should be using a changeset. If not, you should, as it’ll allow you to automatically populate the document which the new upload belongs to.

Secondly, ensure your routes a setup restfully so Phoenix expects a document_id in

defmodule App.Router do
  use App.Web, :router

  scope "/", App do
    resources "/documents", DocumentController do
      resources "/uploads", UploadController

defmodule App.UploadsController do
  use App.Web, :controller

  def new(conn, %{"document_id" => id}) do
    changeset = Upload.changeset(%Upload{document_id: id})
    render(conn, "new.html", changeset: changeset)

The only thing you’ll need to do is ensure is that the User is authorised to manipulate the document id passed in by the form submission in UploadController.create