cobra

cobra

Generating liveviews with ash resources

I’m looking for a way to generate liveviews but with Ash resources instead of the normal ecto schemas?

Thanks in advance :blush:

First 10 of 12 Posts Switch mode

zachdaniel

zachdaniel

Creator of Ash

In the AshPhoenix package there are liveview generators: mix ash_phoenix.gen.live — ash_phoenix v2.3.23

cobra

cobra OP

Why I love Elixir
:heart:
Thanks Zach

Chrichton

Chrichton

I used mix ash_phoenix.gen.live and it worked nicely!
Could you please tell me, what my routes have to look like?

Thanks for your help, Heiko

Chrichton

Chrichton

I created a Phoenix LiveView app using Ash with the following steps:

mix archive.install hex phx_new

mix igniter.new tuneshg \

–install ash,ash_postgres,ash_phoenix \

–with phx.new \

–extend postgres \

–example

Link:

HexDocs Ash Getting started

Create Domain and Resource

mix ash.gen.domain Tuneshg.Music

mix ash.gen.resource Tuneshg.Music.Artist --extend postgres

Define attributes and actions for resource (Music/Artist)

Create migrations - mix ash.codegen create_artists

Run migrations - mix ash.migrate

Generate LiveView

mix ash_phoenix.gen.live --domain Tuneshg.Music --resource Tuneshg.Music.Artist --resourceplural artists


The generated code required to define many routes

Why?

Here my routes:
live “/”, ArtistLive.Index
live “/artists”, ArtistLive.Index
live “/artists/new”, Artist.FormLive, :new
live “/artists/:id”, ArtistLive.Show
live “/artists/:id/edit”, Artist.FormLive, :edit
live “/artists/:id/show/edit”, Artist.FormLive, :edit

The live_action was not added to the socket

Where is the live_action normally set?

Am I doing something wrong/is there a better way?
Thanks for your reply, Heiko

zachdaniel

zachdaniel

Creator of Ash

There was a bug in a recent release of ash_phoenix that prevented it from printing the routes. I’m releasing a fixed version shortly. So now it should tell you which routes to add :slight_smile:

Chrichton

Chrichton

I am working through the new Ash Book from PragPro.
I am trying to follow the book by creating the app from scratch.
I was using ‘mix ash_phoenix.gen.live’ to generate the LiveView from the resources, but that created a LiveComponent, while the example code in the book works mostly with liveviews.

I have two questions:

  1. How do I create an Ash-Liveview?
  2. Why does the generator create a LiveComponent? I thought, I use a LiveComponent, when the View has to be stateful; which means, I have more than one instance of it in my app. But this isn’t the normal case, isn’t it?

Thanks for your answer, Heiko

zachdaniel

zachdaniel

Creator of Ash

While learning to use the LV generator is great, the book was not designed to dovetail off of that generator really. I’m happy to work with you on questions about those generators, but you’re definitely making it harder on yourself by not following the books intended path more closely. There are a lot of concepts that it aims to teach and the focus is not on Liveview. Liveview is primarily a delivery mechanism to teach you about Ash in this context.

Now, the resource LV generator in ash_phoenix should create both a Liveview and a live component for the form. Is that not what you’re seeing?

Chrichton

Chrichton

Sorry, I didn’t that.
I will have a closer look now.
Thanks Zach

Chrichton

Chrichton

After taking a fresh look, I can clarify my question about the generator:
The generator creates Lifeviews to list, show, edit and delete artists.
AND it create a LiveComponent == FormLive to do the actual editing.
My question is about this Component. Why isn’t it a normal LV?
In the Tunez-App in your book you are using a LiveView == FormLive, which is the way, I would have done it. So the question was: Why is the generator not doing the same??

That way I had to convert the LV-Code of the book to my LiveComponent.
Otherwise it would have been easy-peasy.
But I learned a lot by doing so. :slight_smile:

I also ran into one problem which I don’t understand:

The book-code of FormLive looks like this:

def mount(%{"id" => album_id}, _session, socket) do
    album = Tunez.Music.get_album_by_id!(album_id, load: [:artist])

    form = Tunez.Music.form_to_update_album(album)

So I converted that to:

defp assign_form(%{assigns: %{album: album}} = socket) do
    if album do
      artist = Tuneshg.Music.get_artist_by_id!(album.artist_id)
      form = Tuneshg.Music.form_to_update_album(album)

That looked correct, but later, when I tried to edit an album and typed, I got:

[debug] HANDLE EVENT "validate" in TuneshgWeb.AlbumLive.Index
  Component: TuneshgWeb.AlbumLive.FormComponent
  Parameters: %{"_target" => ["form", "name"], "form" => %{"_unused_artist_id" => "", "_unused_cover_image_url" => "", "_unused_name" => "", "_unused_year_released" => "", "artist_id" => "9ad16706-01f7-4b51-8633-21434ba39f21", "cover_image_url" => "/images/albums/nights_in_the_nullarbor_wild.png", "name" => "Wilde", "year_released" => "2023"}}
[error] GenServer #PID<0.772.0> terminating
** (FunctionClauseError) no function clause matching in TuneshgWeb.AlbumLive.FormComponent.handle_event/3

I modified my code to:

form =
        AshPhoenix.Form.for_update(album, :update,
          as: "album",
          transform_params: fn _form, params, _context ->
            Map.put(params, "artist_id", artist.id)
          end
        )

using AshPhoenix.Form.for_update, that I used before and everything worked again.
I don’t know, why the two methods are not equivalent, but I think you do :slight_smile:

My last point:
Yes, you are right, I am making it harder for me to follow :+1: the code in your book and from chapter 3 on, I decided to accept your proposal and follow the book using the Tunez-Code.

Again, dear Zach, thanks a lot for your patience and your time helping me and explaining so superbly, Heiko

zachdaniel

zachdaniel

Creator of Ash

Honestly the LiveView generator was copied directly from phoenix’s generator at the point in time that they were when we made the LiveView generator :laughing:. Ultimately we just want to do whatever LV is doing with their generators, but calling into Ash code instead of Ecto code.

I haven’t looked to see how out of date they are.

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement