How do I return the value

Hello, I’ve managed to get this working…

This should get two inputs from the user and submit it to post function in the controller…

<%= form_for @conn, Routes.page_path(@conn, :averagepercentagecalculate), fn f -> %>

<div class="mb-3 mt-2">
    <label class="form-label">Percentage 1: </label>
    <%= text_input f, :percentageone, class: "form-control" %>
</div>

<div class="mb-3 mt-2">
    <label class="form-label">Percentage 2: </label>
    <%= text_input f, :percentagetwo, class: "form-control" %>
</div>

  <%= submit "Submit", class: "btn btn-primary" %>
<% end %>

I am able to access the value from params, but Im not able to send it to my template…

def averagepercentagecalculate(conn, params) do
    pone = Map.fetch!(params, "percentageone")
    render(conn, "averagepercentage.html", pone: pone)
  end

Is there a workaround?

what do you mean you can’t send it to your template?

have you used <%= @pone %> in your template?

Yeah I did, I just got a big *ss error message.

can you post the error and your template?

My apologies, looks like the issue was on my end. I forgot to close a form with “%>”.
However, thank you :slight_smile:.

Oh well, looks like its back.

The error message:

Error Message

[error] #PID<0.5558.0> running MathmonkeyWeb.Endpoint (connection #PID<0.5493.0>, stream id 5) terminated
Server: localhost:4000 (http)
Request: GET /average-percentage
** (exit) an exception was raised:
** (KeyError) key :pone not found in: %{conn: %Plug.Conn{adapter: {Plug.Cowboy.Conn, :…}, assigns: %{layout: {MathmonkeyWeb.LayoutView, “app.html”}}, body_params: %{}, cookies: %{"_mathmonkey_key" => “SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYOUo5RTJvaFN2eHZzZXBuZkEybEJUTWpG.8eClebUmG_8gtAEnSSt_i68wQ4M_d1piA5R4YyeKUlM”}, halted: false, host: “localhost”, method: “GET”, owner: #PID<0.5558.0>, params: %{}, path_info: [“average-percentage”], path_params: %{}, port: 4000, private: %{MathmonkeyWeb.Router => {[], %{Plug.Swoosh.MailboxPreview => [“mailbox”]}}, :before_send => [#Function<0.16477574/1 in Plug.CSRFProtection.call/2>, #Function<2.17183421/1 in Phoenix.Controller.fetch_flash/2>, #Function<0.77458138/1 in Plug.Session.before_send/2>, #Function<0.23023616/1 in Plug.Telemetry.call/2>, #Function<1.46227650/1 in Phoenix.LiveReloader.before_send_inject_reloader/3>], :phoenix_action => :averagepercentage, :phoenix_controller => MathmonkeyWeb.PageController, :phoenix_endpoint => MathmonkeyWeb.Endpoint, :phoenix_flash => %{}, :phoenix_format => “html”, :phoenix_layout => {MathmonkeyWeb.LayoutView, :app}, :phoenix_request_logger => {“request_logger”, “request_logger”}, :phoenix_root_layout => {MathmonkeyWeb.LayoutView, :root}, :phoenix_router => MathmonkeyWeb.Router, :phoenix_template => “averagepercentage.html”, :phoenix_view => MathmonkeyWeb.PageView, :plug_session => %{"_csrf_token" => “9J9E2ohSvxvsepnfA2lBTMjF”}, :plug_session_fetch => :done}, query_params: %{}, query_string: “”, remote_ip: {127, 0, 0, 1}, req_cookies: %{"_mathmonkey_key" => “SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYOUo5RTJvaFN2eHZzZXBuZkEybEJUTWpG.8eClebUmG_8gtAEnSSt_i68wQ4M_d1piA5R4YyeKUlM”}, req_headers: [{“accept”, “text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8”}, {“accept-encoding”, “gzip, deflate”}, {“accept-language”, “en-US,en;q=0.9”}, {“cache-control”, “max-age=0”}, {“connection”, “keep-alive”}, {“cookie”, “_mathmonkey_key=SFMyNTY.g3QAAAABbQAAAAtfY3NyZl90b2tlbm0AAAAYOUo5RTJvaFN2eHZzZXBuZkEybEJUTWpG.8eClebUmG_8gtAEnSSt_i68wQ4M_d1piA5R4YyeKUlM”}, {“host”, “localhost:4000”}, {“referer”, “http://localhost:4000/average-percentage”}, {“upgrade-insecure-requests”, “1”}, {“user-agent”, “Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.3 Safari/605.1.15”}], request_path: “/average-percentage”, resp_body: nil, resp_cookies: %{}, resp_headers: [{“cache-control”, “max-age=0, private, must-revalidate”}, {“x-request-id”, “FuMWhbFtgCRR9GAAAGGB”}, {“x-frame-options”, “SAMEORIGIN”}, {“x-xss-protection”, “1; mode=block”}, {“x-content-type-options”, “nosniff”}, {“x-download-options”, “noopen”}, {“x-permitted-cross-domain-policies”, “none”}, {“cross-origin-window-policy”, “deny”}], scheme: :http, script_name: [], secret_key_base: :…, state: :unset, status: nil}}
(mathmonkey 0.1.0) lib/mathmonkey_web/templates/page/averagepercentage.html.heex:15: anonymous fn/2 in MathmonkeyWeb.PageView.“averagepercentage.html”/1
(phoenix_live_view 0.17.7) lib/phoenix_live_view/engine.ex:124: Phoenix.HTML.Safe.Phoenix.LiveView.Rendered.to_iodata/1
(phoenix_live_view 0.17.7) lib/phoenix_live_view/engine.ex:140: Phoenix.HTML.Safe.Phoenix.LiveView.Rendered.to_iodata/3
(phoenix 1.6.6) lib/phoenix/controller.ex:772: Phoenix.Controller.render_and_send/4
(mathmonkey 0.1.0) lib/mathmonkey_web/controllers/page_controller.ex:1: MathmonkeyWeb.PageController.action/2
(mathmonkey 0.1.0) lib/mathmonkey_web/controllers/page_controller.ex:1: MathmonkeyWeb.PageController.phoenix_controller_pipeline/2
(phoenix 1.6.6) lib/phoenix/router.ex:355: Phoenix.Router.call/2
(mathmonkey 0.1.0) lib/mathmonkey_web/endpoint.ex:1: MathmonkeyWeb.Endpoint.plug_builder_call/2
(mathmonkey 0.1.0) lib/plug/debugger.ex:136: MathmonkeyWeb.Endpoint.“call (overridable 3)”/2
(mathmonkey 0.1.0) lib/mathmonkey_web/endpoint.ex:1: MathmonkeyWeb.Endpoint.call/2
(phoenix 1.6.6) lib/phoenix/endpoint/cowboy2_handler.ex:54: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy 2.9.0) /Users/khaled/Documents/MathDay/MathMonkey/mathmonkey/deps/cowboy/src/cowboy_handler.erl:37: :cowboy_handler.execute/2
(cowboy 2.9.0) /Users/khaled/Documents/MathDay/MathMonkey/mathmonkey/deps/cowboy/src/cowboy_stream_h.erl:306: :cowboy_stream_h.execute/3
(cowboy 2.9.0) /Users/khaled/Documents/MathDay/MathMonkey/mathmonkey/deps/cowboy/src/cowboy_stream_h.erl:295: :cowboy_stream_h.request_process/3
(stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

The template:

<%= form_for @conn, Routes.page_path(@conn, :averagepercentagecalculate), fn f -> %>

<div class="mb-3 mt-2">
    <label class="form-label">Percentage 1: </label>
    <%= text_input f, :percentageone, class: "form-control" %>
</div>

<div class="mb-3 mt-2">
    <label class="form-label">Percentage 2: </label>
    <%= text_input f, :percentagetwo, class: "form-control" %>
</div>

  <%= submit "Submit", class: "btn btn-primary" %>
<% end %>
<%= @pone %>

do you use the same view after posting? if yes, then when you mount you won’t have that variable available (unless you set it before returning the view).

So you probably need to check if it’s there before trying to render it.

Since I have two functions, one which displays the page during the GET event, and one that displays the page during the POST event. I have set it to ‘0’ for the GET, and for the POST it’s going to give the user his desired outcome.

Thanks.