hackersleepyhead

hackersleepyhead

Phoenix LiveView and LiveComponent assign always empty map

I am trying to build a normal CRUD application using Phoenix.LiveView, Here I am facing an issue in passing or setting a Value is ASSIGN or IN SOCKET.

I have a LiveView code

Index.ex

defmodule Example.Index do
	use Phoenix.LiveView

	@default_state %{
		is_editing?: false,
		user_id: nil,
	}

	def render(assign) do
		ExampleWeb.render("index.html", assign)
	end

	def mount(_params, _session, socket) do
		users = Example.get_users()
		{:ok, assign(socket, %{@default_state | users: users})}
	end

	def handle_event("edit-user", %{"user-id" => user_id}, socket) do
		new_socket =
			socket
			|> assign(:user_id, user_id)

		{:noreply,
			live_redirect(new_socket,
			to: ExampleWeb.Router.Helpers.users_edit_path(new_socket, __MODULE__, user_id),
			replace: false
		)}
	end
end

Template file

<div class="card">
  <%= if @is_editing? do %>
    <%= live_component(@socket, ExampleWeb.Users.EditFormComponent, id: :edit_user) %>
  <%= end %>
  <%= if @is_editing? != true do %>
    <div class="card-header">
      <strong>Users</strong>
      <a class="btn btn-sm btn-success ml-3 pull-right" phx-click="new">
        <i class="fa fa-plus mr-1"></i> Create
      </a>
    </div>
   <%= end %>
</div

here is my component.ex code

defmodule ExampleWeb.Users.EditFormComponent do
	use Phoenix.LiveComponent

	def render(assigns) do
		UserView.render("update.html", assigns)
	end
	
	def mount(socket) do
		{:ok, assign(socket, changeset: User.changeset(%User{}))}
	end
end

the problem is here I am not able to get the user-id which is set/update in LiveView file and also not able to access the PARAMS component level.

Can somebody suggest or provide a solution for this??

First Post!

sfusato

sfusato

You’re not passing the user_id to your component:

    <%= live_component(@socket, ExampleWeb.Users.EditFormComponent, id: :edit_user) %>

Last Post!

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

can you show more complete code for both views and the component?

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement