potentElixir
In the post/show template I have:
<%= for comment <- @comments do %>
if (condition) do
# pass comment to _scripts.html
end
end
The objective is that in the _scripts.html, I need the comment available. (_scripts.html has javascript associated with the template)
By default, if I don’t do anything then the render line in post controller render(conn, "show.html", post: post, comments: comments) gives _scripts.html the Available assigns: [:conn, :post, :comments, ...].
So therefore to inject comment into the available assigns I do:
if (condition) do
# pass comment to _scripts.html
<%= render "_scripts.html", Map.put(@conn.assigns, :comment, comment) %>
end
which gives available assigns: [:comment, :post, :comments, ...] but the conn goes missing.
So, instead I tried doing:
if (condition) do
# pass comment to _scripts.html
<%= render "_scripts.html", Map.merge(@conn.assigns, %{comment: comment, conn: @conn}) %>
end
but then conn is available while comment goes missing, as in: assign @comment not available in eex template. Available assigns: [:conn, :post, :comments, ...]
Any help on this will be appreciated.
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
grych
I think you are confusing
conn.assignswith template assigns. Try simply:potentElixir
Hey @grych, I tried your solution but it didnt work, I still get
assign @comment not available in eex template.Here on SO I found, a community member explains the difference between assigns and conn.assigns.
grych
Please share your exact code, you must have done some other mistake there.
@commentshould be available in the"_script.html"when you pass it torender/2PJextra
You’re right but I think there’s a typo…try:
NobbZ
Nope, that won’t work. Please take a look at the OP, there is a comprehension missing parts of the implementation, while later on that call to
render/2is revealed as beeing the implementation.grych
As I understood from the code sample at the first post,
commentis a local variable, not an assign.PJextra
Yes, right. I didn’t see the code sample carefully.
potentElixir
Hi @grych, I think you are wondering if the condition evaluates to true, which dictates if the comment will be passed to _scripts.html. The condition does not change, and the line
<%= render "_scripts.html", Map.put(@conn.assigns, :comment, comment) %>does give @comment to _scripts.html but @conn goes missing.Any ways I realized two things, firstly while testing around other methods like doing:
<%= render "_scripts.html", conn: @conn, post: @post, comment: comment %>that it works when I do the above line, but then the
_scripts.htmlfile complains @current_user not available (while conn, post and comment are available).Then to resolve that I do:
<%= render "_scripts.html", conn: @conn, post: @post, comment: comment, current_user: @current_user %>and it says @comment not available.
The second thing I realized is that even if
commentwere available from theforloop in the post/show template to the _scripts.html its only 1 comment (the first or the last comment) available at page load, since javascript is loaded.So what I was trying to eventually do in the _scripts.html file, that is to save the reply to a comment as a draft in localStorage, in case the user navigates away accidentally from the individual post show page, would probably not work with that implementation.
OvermindDL1
Uhhh what? Why are you taking the plug’s
assignsand passing it as the viewassigns, those are two different set of assigns, and thus of course@connwould go missing. That render call is definitely wrong, it should be more like:(Well you could pass in
@assigns,@assigns!=@conn.assignfor note,@assignsis what was actually passed into the template.)Correct, you have to pass in everything to a render call explicitly.
@commentshould most definitely be accessible within"_script.html"with that render call.Seems like it should work.
potentElixir
Hey @grych, @PJextra, @NobbZ and @OvermindDL1 , thanks for all you guys input, doing
<%= render "_scripts.html", conn: @conn, post: @post, comment: comment %>finally worked (duh…what a hard and complicated solution to finally arrive at…eh). I just tackled the current_user problem in an another way. @OvermindDL1 thanks man for explaining it and getting the head think straight.