How to pass data from one function to another in a controller

hi, I would like to pass from the new function that list of payments to the create function as I can pass it?

  def new(conn, %{"transaction" => %{"payments_ids" => payments_ids}}) do
        payment_id = select_payment(payments_ids)
        render(conn, "signer.html", payment_id: payment_id)
    end
 def create(conn, %{"assert" => assert, "private_key" => private_key}) do
                transaction= Accounts.create_transaction(payment_id) 
                conn
                |> put_flash(:info`Preformatted text`, " transaction made ")
                |> redirect(to: Routes.payment_list_path(conn, :index))
            
    end

I thought about passing it through the template but I did not succeed

<center>
<h1>Modal</h1>
</center>
<%= form_for @conn, Routes.signer_path(@conn, :create), fn f -> %>
<label>
 Assert: <%= select f, :assert, ["mtk20", "USD", "EUR"]  %>
   </label>
 <br>
 <label>
 Private Key: <%= password_input f, :private_key %>
   </label>


  <%= submit "Sign" %>
<% end %>

I think the easiest way for you would be getting this data the same way you get it in the new action of your controller. Then if you don’t want to repeat the code just write it in a plug function that will be called before new and create actions.

# defining private plug function somewhere at the bottom of the controller
defp assign_payment(%{params: %{"payments_ids" => payments_ids}} = conn, _opts) do
  payment = get_payment!(payments_ids)
  assign(conn, :payment_id, payment)
end

# somewhere in the top of the controller before all actions definition
plug assign_payment when action in [:new, :create]

# to get payment_id in both "new" and "create" actions
conn.assigns.payment_id

Edit : Please note that with this plug, payment_id will also be available in the template linked to both actions.

For an in depth-use this article may be of interest: Alternatives parameters for Phoenix controller actions.

and I must put the payment_assignment

def new(conn, %{"transaction" => %{"payments_ids" => payments_ids}}) do
    # payment_id = Accounts.create_transaction(%Transaction{}, %{operations: get_pending_payments(payments_ids))
    payments = select_payment(payments_ids)
    conn
    |> assign(:payments_list, payments)
    |> render("signer.html")
  end

  def create(conn, %{"assets" => assets, "private_key" => private_key}) do
    {:ok, transaction} = Accounts.create_transaction(conn.assigns.payments_list)

    conn
    |> put_flash(:info, " transaction made " <> transaction.id)
    |> redirect(to: Routes.payment_list_path(conn, :index))
  end

or that payment_assignment which function fulfills, because I have the list of payments in new and when calling the payment_assign I get errors by the parameters

Sorry, in my previous reply the plug function arguments should match the request params to retrieve payment_ids. So the right matching is rather:

defp assign_payment(%{params: %{"transaction" => %{"payments_ids" => payments_ids}}} = conn, _opts) do
  payment = get_payment!(payments_ids)
  assign(conn, :payment_id, payment)
end

I don’t know how you send this param in the first place to the new action, but you need to ensure that it’s sent also to the create action.

Edit: If you don’t want to or can’t get the data that way, you can try to use Plug.Session.