wlminimal

wlminimal

Drab: how to get a conn value?

Is there anyway to get conn value inside defhandler in drab?
I want to get a user from conn.assigns like this

defhandler shorten_url(socket, sender) do
    long_url = sender.params["long_url_textarea"]
    user = conn.assigns.current_user
    case Analytics.create_short_link(long_url) do
      {:ok, results} ->
        # Create bitly struct
        %{id: bitlink_id, link: bitlink_url, long_url: long_url} = results
        Bitly.create_bitly(%{bitlink_id: bitlink_id, bitlink_url: bitlink_url, long_url: long_url, total_clicks: 0, user_id: user.id})

        poke socket, long_url: bitlink_url
      {:error, errors} ->
        set_prop socket, "#long-url-error", innerHTML: "Something wrong found in your url. Make sure including http://, for example http://www.example.com"
    end
  end

Most Liked

grych

grych

Creator of Drab

You can’t. Conn does not exists anymore - it is available only when rendering page with controller. While the page is already rendered and connected, the similar role for conn plays socket

To get the userid, you may use:

# app.html.eex:
<%= Drab.Client.run(@conn, current_user: @conn.assigns.current_user) %>

# your_commander.ex
defhandler shorten_url(socket, sender) do
    user = socket.assigns.current_user
end
grych

grych

Creator of Drab

Actually, I can’t reproduce this issue. In my environment all works as expected: the other form values (name, description) are kept.

The only reason why this value may be cleared, is when Drab decides to refresh them. This may be because the form may be under some other expression. Imagine something like:

<%= if @short_url do %>
  <input id=other>
  <input value=<%= @long_url%> id=long_url>
  <input value=<%= @short_url%> id=short_url>
<% end %>

Now, when you poke socket, long_url: ..., everything will work as expected: the only <input id=long_url> is going to be updated. So the other input value will be untouched.

But what when you poke socket, short_url: ...? Drab will update everything under if, so all three inputs will be refreshed! This will re-set their values.

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New

Other popular topics Top

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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement