wlminimal
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
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
grych
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
connplayssocketTo get the userid, you may use:
socket.assigns- you may add the assign to thesocketstruct when rendering Drab JS inapp.html.eex, eg:grych
Actually it is not complicated to get
conn.assignscopied tosocket.assigns(with some whitelisting to avoid storing large structs). If you feel it would be helpful, please make an issue on github. I did not think about this before, as IMO session provides better functionality for such stuff.wlminimal
I tried first solution
in config.exs I tried this but got an error
so I did
in commander.ex
but it returns nil
and section solution
I tried what you exactly said. but in browser console it says
What am I missing?
grych
Oh, there is an error in the documentation, thanks for pointing it out!
It might be a stupid question, but how do you set the session value in the controller? Can you share this part?
It is hard to say, is you code available somewhere like github, so I could take a look?
wlminimal
I put the user id session when user logs in like this
and using plug to check if user_id is in session like this
in router.ex
wlminimal
one more question.
I am trying to use drab to shorten url.
There is a form that has several input field.
and when I click a button to shorten url using drab, but text in other field refreshed and get emptied.
it doesn’t look like a refreshing page. but it clear all the other field.
is this normal behavior?
I put those in two separate form element.
grych
Strange, all looks OK. Let’s try to debug it.
socket.assigns[:__session]?Drab.Config.get(MyAppWeb.Endpoint, :access_session)?grych
It might be that your handler function updates not only the requested field, but also the whole form. Could you please share the form and the commander handler function?
wlminimal
and defhandler here
wlminimal
in config.exs
in commander.ex
return nil