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
You’re not passing the user_id to your component:
<%= live_component(@socket, ExampleWeb.Users.EditFormComponent, id: :edit_user) %>
0
Last Post!
benwilson512
Author of Craft GraphQL APIs in Elixir with Absinthe
Popular in Questions
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
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
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
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
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
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
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
Hello, how can I check the Phoenix version ?
Thanks !
New
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
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









