add 記事の追加

This commit is contained in:
ReyoKatsu 2025-04-19 17:21:36 +09:00
parent 2031881803
commit 1f3124f3f8
No known key found for this signature in database
GPG key ID: FD0DBDFC4A7B6EE5
3 changed files with 32 additions and 25 deletions

View file

@ -65,6 +65,7 @@ defmodule PhoenixRealWorldWeb.ArticleLive.FormComponent do
end end
defp save_article(socket, :new, article_params) do defp save_article(socket, :new, article_params) do
article_params = Map.put(article_params, "user_id", socket.assigns.current_user.id)
case Blogs.create_article(article_params) do case Blogs.create_article(article_params) do
{:ok, article} -> {:ok, article} ->
notify_parent({:saved, article}) notify_parent({:saved, article})

View file

@ -18,16 +18,16 @@
<div class="sr-only"> <div class="sr-only">
<.link navigate={~p"/articles/#{article}"}>Show</.link> <.link navigate={~p"/articles/#{article}"}>Show</.link>
</div> </div>
<.link patch={~p"/articles/#{article}/edit"}>Edit</.link> <%!-- <.link patch={~p"/articles/#{article}/edit"}>Edit</.link> --%>
</:action> </:action>
<:action :let={{id, article}}> <%!-- <:action :let={{id, article}}>
<.link <.link
phx-click={JS.push("delete", value: %{id: article.id}) |> hide("##{id}")} phx-click={JS.push("delete", value: %{id: article.id}) |> hide("##{id}")}
data-confirm="Are you sure?" data-confirm="Are you sure?"
> >
Delete Delete
</.link> </.link>
</:action> </:action> --%>
</.table> </.table>
<.modal :if={@live_action in [:new, :edit]} id="article-modal" show on_cancel={JS.patch(~p"/articles")}> <.modal :if={@live_action in [:new, :edit]} id="article-modal" show on_cancel={JS.patch(~p"/articles")}>
@ -37,6 +37,7 @@
title={@page_title} title={@page_title}
action={@live_action} action={@live_action}
article={@article} article={@article}
current_user={@current_user}
patch={~p"/articles"} patch={~p"/articles"}
/> />
</.modal> </.modal>

View file

@ -24,7 +24,12 @@ defmodule PhoenixRealWorldWeb.ArticleLiveTest do
end end
test "saves new article", %{conn: conn} do test "saves new article", %{conn: conn} do
{:ok, index_live, _html} = live(conn, ~p"/articles") # {:ok, index_live, _html} = live(conn, ~p"/articles")
#↓追加
{:ok, index_live, _html} =
conn
|> log_in_user(user_fixture())
|> live(~p"/articles")
assert index_live |> element("a", "New Article") |> render_click() =~ assert index_live |> element("a", "New Article") |> render_click() =~
"New Article" "New Article"
@ -46,35 +51,35 @@ defmodule PhoenixRealWorldWeb.ArticleLiveTest do
assert html =~ "some title" assert html =~ "some title"
end end
test "updates article in listing", %{conn: conn, article: article} do # test "updates article in listing", %{conn: conn, article: article} do
{:ok, index_live, _html} = live(conn, ~p"/articles") # {:ok, index_live, _html} = live(conn, ~p"/articles")
assert index_live |> element("#articles-#{article.id} a", "Edit") |> render_click() =~ # assert index_live |> element("#articles-#{article.id} a", "Edit") |> render_click() =~
"Edit Article" # "Edit Article"
assert_patch(index_live, ~p"/articles/#{article}/edit") # assert_patch(index_live, ~p"/articles/#{article}/edit")
assert index_live # assert index_live
|> form("#article-form", article: @invalid_attrs) # |> form("#article-form", article: @invalid_attrs)
|> render_change() =~ "can&#39;t be blank" # |> render_change() =~ "can&#39;t be blank"
assert index_live # assert index_live
|> form("#article-form", article: @update_attrs) # |> form("#article-form", article: @update_attrs)
|> render_submit() # |> render_submit()
assert_patch(index_live, ~p"/articles") # assert_patch(index_live, ~p"/articles")
html = render(index_live) # html = render(index_live)
assert html =~ "Article updated successfully" # assert html =~ "Article updated successfully"
assert html =~ "some updated title" # assert html =~ "some updated title"
end # end
test "deletes article in listing", %{conn: conn, article: article} do # test "deletes article in listing", %{conn: conn, article: article} do
{:ok, index_live, _html} = live(conn, ~p"/articles") # {:ok, index_live, _html} = live(conn, ~p"/articles")
assert index_live |> element("#articles-#{article.id} a", "Delete") |> render_click() # assert index_live |> element("#articles-#{article.id} a", "Delete") |> render_click()
refute has_element?(index_live, "#articles-#{article.id}") # refute has_element?(index_live, "#articles-#{article.id}")
end # end
end end
describe "Show" do describe "Show" do