lcabrini

lcabrini

I have a schema with a status field, that can be :draft, :published, etc. In the index live view, I only want the delete link to be available for items that have not been published, that is they are :draft.

I thought this should be extremely simple:

<%= if article.status == :draft do %>
  <:action :let={{id, article}}>
    <.link
      phx-click={JS.push("delete", value: %{id: article.id}) |> hide("##{id}")}
      data-confirm="Are you sure?"
    >
      Delete
    </.link>
  </:action>
<% end %>

However, this tells me:

== Compilation error in file lib/foobar_web/live/article_live/index.ex ==
** (Phoenix.LiveView.Tokenizer.ParseError) lib/foobar_web/article_live/index.ex:37:11: invalid slot entry <:action>. A slot entry must be a direct child of a component
   |
34 |               Delete
35 |             </.link>
36 |           </:action>
37 |         <% end %>
   |           ^

I don’t really get it. As far as I understand, it IS a direct child of a component, all that exists above it is a logic statement, a “preprocessor statement”, which should have been evaluated in an initial pass, or?

In any case, I accept that for some reason this doesn’t work.. But then, how would I achieve what I’m trying to do? It seems common enough that I imagine this should be easy to do. I can’t seem to find the right keywords to search on.

If I re-arrange it a bit so that the <% if … %> is embedded inside the <:action …>, then it does work. Is that the best way to do it?

Showing Posts 1 to 10

matt-savvy

matt-savvy

What happens if you do it like this:?

<:action :if={condition}>
    ...
</:action>
FlyingNoodle

FlyingNoodle

The error message tells you the issue:

you need to put your conditional inside the slot.

LostKobrakai

LostKobrakai

or better on the slot using :if as suggested by @matt-savvy. that way the slot is not passed with empty content to the component.

garrison

garrison

Tbh this is what convinced me that named slots are not a very good API. They’re a cool idea but they don’t hold up because they don’t compose properly. The introduction of :if and :for is, IMO, an ugly bandaid. Components are a more versatile primitive.

But yeah, as mentioned above, you have to use :if (or potentially :for) if you want to elide the slot itself from the output.

Alternatively you could ditch slots and use a component like <.article_action /> and then your intuitive solution would actually work.

lcabrini

lcabrini OP

This does look like the proper way to solve it.

lcabrini

lcabrini OP

Of course there was going to be a simple and logical solution. And of course my mind was not going to figure it out. Thanks.

lcabrini

lcabrini OP

Maybe I wrote a bit too soon.

<:action :let={{id, article}} :if={article.status == :draft}>

Gives me: undefined variable “article”. Which one get evaluated first, the :let or the :if? Or more to the point, how to I use article in the conditional?

garrison

garrison

In the OP you are using an article from the outer scope and then rebinding the variable inside the :let. Where did the outer article go?

This is kinda just proving my point, though. I have absolutely no idea how precedence is handled between :let and :if because it’s not real code, it’s a made up template language. So now we’re both confused.

(Just gonna casually tag @bartblast (hi) because this is a pretty clear real-world example of why I am anti-template, which he may appreciate.)

lcabrini

lcabrini OP

Since this is just an experimental project where I’m trying different things out, I was going to try both solutions. I wanted to start with the one @matt-savvy proposed and then yours. Apart from the :if clause I added, this is standard index view that phx.gen.live would create. I’m guessing that the outer article “variable” is created by <.table>.

The only way I can understand why the variable is not available to the :if would be that it creates new scope that knows nothing about article. If that is the case, then I don’t understand what the use of the conditional clause would be.

garrison

garrison

The :if solution is still idiomatic even if I don’t (personally) like that it exists.

But what I’m asking is, in the OP, what is the article in the if statement? We can’t see the rest of the code. Where does it come from?

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
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
velrest
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
samoloth
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
FlyingNoodle
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 Top

JesseHerrick
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews