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) %>

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is qui...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "eq...
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
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30840 112
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126226 1237
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement