Skip to content

Commit

Permalink
docs: update docs w/ new example
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Dec 22, 2024
1 parent 862c395 commit 9fa16fd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 88 deletions.
2 changes: 2 additions & 0 deletions documentation/dsls/DSL-AshPhoenix.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ This file was generated by Spark. Do not edit it by hand.

An extension to add form builders to the code interface.

There is currently no DSL for this extension.

This defines a `form_to_<name>` function for each code interface
function. Positional arguments are ignored, given that in forms,
all input typically comes from the `params` map.
Expand Down
97 changes: 9 additions & 88 deletions documentation/tutorials/getting-started-with-ash-and-phoenix.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@ defmodule MyAshPhoenixApp.Blog.Post do
defaults [:read, :destroy]

create :create do
primary? true
# accept title as input
accept [:title]
end

update :update do
primary? true
# accept content as input
accept [:content]
end
Expand Down Expand Up @@ -317,97 +319,16 @@ Now isn't that more convenient?
## Connecting your Resource to a Phoenix LiveView
Now we know how to interact with our resource, let's connect it to a simple Phoenix LiveView. Here is the LiveView below. If you are using phoenix live_view <= 0.18, you will need to use `let={}` instead of `:let={}`.
Now we know how to interact with our resource, we can generate a liveview for it!
let's run `mix ash_phoenix.gen.live` to generate a liveview! Run the following command,
accepting any default values and following the instructions listed at the end for adding
the liveview to your router.
```elixir
# lib/my_ash_phoenix_app_web/posts_live.ex
defmodule MyAshPhoenixAppWeb.PostsLive do
use MyAshPhoenixAppWeb, :live_view
alias MyAshPhoenixApp.Blog
alias MyAshPhoenixApp.Blog.Post
def render(assigns) do
~H"""
<h2 class="text-xl text-center">Your Posts</h2>
<div class="my-4">
<div :if={Enum.empty?(@posts)} class="font-bold text-center">
No posts created yet
</div>
<ol class="list-decimal">
<li :for={post <- @posts} class="mt-4">
<div class="font-bold"><%= post.title %></div>
<div><%= if Map.get(post, :content), do: post.content, else: "" %></div>
<button
class="mt-2 p-2 bg-black text-white rounded-md"
phx-click="delete_post"
phx-value-post-id={post.id}
>
Delete post
</button>
</li>
</ol>
</div>
<h2 class="mt-8 text-lg">Create Post</h2>
<.form :let={f} for={@create_form} phx-submit="create_post">
<.input type="text" field={f[:title]} placeholder="input title" />
<.button class="mt-2" type="submit">Create</.button>
</.form>
"""
end
def mount(_params, _session, socket) do
posts = Blog.list_posts!()
socket =
assign(socket,
posts: posts,
post_selector: post_selector(posts),
create_form: AshPhoenix.Form.for_create(Post, :create) |> to_form()
)
{:ok, socket}
end
def handle_event("delete_post", %{"post-id" => post_id}, socket) do
post_id |> Blog.get_post!() |> Blog.destroy_post!()
posts = Blog.list_posts!()
{:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}
end
def handle_event("create_post", %{"form" => form_params}, socket) do
case AshPhoenix.Form.submit(socket.assigns.create_form, params: form_params) do
{:ok, _post} ->
posts = Blog.list_posts!()
{:noreply, assign(socket, posts: posts, post_selector: post_selector(posts))}
{:error, create_form} ->
{:noreply, assign(socket, create_form: create_form)}
end
end
defp post_selector(posts) do
for post <- posts do
{post.title, post.id}
end
end
end
```
Don't forget to add the LiveView to your router.
```elixir
# lib/my_ash_phoenix_app_web/router.ex
scope "/", MyAshPhoenixAppWeb do
# ...
live "/posts", PostsLive
end
```bash
mix ash_phoenix.gen.live MyAshPhoenixApp.Blog MyAshPhoenixApp.Blog.Post --resource-plural posts
```
Now, start the web server by running `mix phx.server`. Then, visit http://localhost:4000/posts in your browser to see what we have just created.
Now, start the web server by running `mix phx.server`. Then, visit the posts route that you added in your browser to see what we have just created.
You can see how using functions created by our `code_interface` makes it easy to integrate Ash with Phoenix.
Expand Down

0 comments on commit 9fa16fd

Please sign in to comment.