ltgaxkeh

ltgaxkeh

Hello! Sorry for the dumb question but how do I display a variable in template. Let’s say I have an input in template and a button for submitting it. Once submitting, the value that I entered in my input passes to a controller. I was able to get the value through the controller and my problem is how do I display it on the same template?

Showing Posts 1 to 7

joaoevangelista

joaoevangelista

do you want to show the value in the template your controller action returns? if so then the you need to pass your value in teh render/3 function, for example:

Given a controller:

defmodule MyAppWeb.MyController do
  use MyAppWeb, :controller


  def new(conn, params = %{"value" => my_value}) do
   # do your stuff
   render(conn, "new.html", value: my_value)
  end

end

render/3 accepts a keyword list or a map as last argument, so anything you need to send to template you put there with a key, so on you template you can access it as

<span><%= @value %></span>

btw, there is no such thing as dumb question! :smile:

ltgaxkeh

ltgaxkeh OP

I gott the error:

assign @value not available in eex template.

mhanberg

mhanberg

Expert LSP Core Team

Do you mind sharing your controller and template code?

Narven

Narven

Most of the times, try restarting mix phx.server.

chrisdel101

chrisdel101

I know this is quite old, but I have this exact same problem, and I’m stuck.

def new(conn, _params) do
      changeset = Administration.change_admin(%Admin{})

      admin_roles = [1,2,3]

      render(conn, "new.html", changeset: changeset, admin_roles: admin_roles)
    end

Then in html <%= admin_roles %>

Error: variable "admin_roles" does not exist
`

UPDATE: realized I need @admin_roles in template

karlosdaniel451

karlosdaniel451

I’m trying to pass a “users” variable but I’m receiving the error

KeyError <small>at GET</small> <small>/agendas/new</small>

key :users not found in: %{
  action: "/agendas",
  __changed__: nil,
  __given__: %{
    action: "/agendas",
    __changed__: nil,
    changeset: #Ecto.Changeset<
      action: nil,
      changes: %{},
      errors: [
        data: {"can't be blank", [validation: :required]},
        hora: {"can't be blank", [validation: :required]}
      ],
      data: #Vacinacao.Agendas.Agenda<>,
      valid?: false
    >
  },
  changeset: #Ecto.Changeset<
    action: nil,
    changes: %{},
    errors: [
      data: {"can't be blank", [validation: :required]},
      hora: {"can't be blank", [validation: :required]}
    ],
    data: #Vacinacao.Agendas.Agenda<>,
    valid?: false
  >
}
kokolegorille

kokolegorille

Hello and welcome, please use 3 ` to format your code. I did the change this time.

You don’t show how You try to pass the users to the template. You should, because we don’t know your code

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews