Share a variable from one function to another in a controller through assign

Hi! I am trying to pass that payment_list list of the new function where I receive it through a different template to the create function through the assign but I can’t do it because it returns a nil
I am doing it as follows:

Here I evolve it from a different template

<form method="get" action = <%=Routes.signer_path(@conn, :new, %{"transaction" => %{"payments_ids" => [1, 2, 3, 4]}})%>>

This is the new one that receives that payment id list and converts it into a payment list, then I save it through assign and then be used in the create function

def new(conn, %{"transaction" => %{"payments_ids" => payments_ids}}) do
    payments = select_payment(payments_ids)
    |> assign(:payments_list, payments)
    |> render("signer.html")

and this is the create function that receives that payment list

def create(conn, %{"assets" => assets, "private_key" => private_key}) do
    {:ok, transaction} = Accounts.create_transaction(conn.assigns[:payments_list])
    IO.inspect(conn.assigns[:payments_list], layaut: "hola")
    |> put_flash(:info, " transaction made " <>
    |> redirect(to: Routes.payment_list_path(conn, :index))

but that conn.assigns [: payments_list], is returning a nil

Assigns do not persist between requests. You need to persist that data in a cookie, session or form field in-between.


and how can persist that data in a cookie, session? I’m new at this!!

*form field in-between. -This refers to sending it by the render to receive it in the template with an @ … and send it to the create by signer_path?

Probably the simplest thing is to add a hidden input in the new form. This hidden input would hold payments_ids, and this would be available in the create action…

Anyway Your setup is highly non-conventional, and You are mixing with REST rules. New should be a get request, but because of your setup, it’s a post.

In fact, it is looking like a multiple steps form.

1 Like

how do I add a hidden input in the new form?

Cou can find doc for hidden input here