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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement