本文介绍了POST 时的 Phoenix.ActionClauseError,没有匹配的操作子句来处理请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我 POST 到/api/subastas 时,我都会收到这个错误
I'm receiving this error whenever I POST to /api/subastas
Phoenix.ActionClauseError at POST /api/subastas/ bad request to IascSubastas.SubastaController.create, no matching action clause to process request
如果我运行 mix phoenix.routes,我会看到 :create 正确路由到 POST/api/subastas
If I run mix phoenix.routes I see that :create is correctly routed to POST /api/subastas
这是 router.ex
This is the router.ex
defmodule IascSubastas.Router do
use IascSubastas.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", IascSubastas do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
end
# Other scopes may use custom stacks.
scope "/api", IascSubastas do
pipe_through :api
resources "/subastas", SubastaController, except: [:new, :edit] do
post "/cancelar", SubastaController, :cancelar
resources "/ofertas", OfertaController, except: [:new, :edit]
end
end
end
和 SubastaController
and SubastaController
defmodule IascSubastas.SubastaController do
use IascSubastas.Web, :controller
alias IascSubastas.Subasta
def index(conn, _params) do
subastas = Repo.all from s in Subasta, preload: [:mejor_oferta]
render(conn, "index.json", subastas: subastas)
end
def show(conn, %{"id" => id}) do
subasta = Repo.get!(Subasta, id) |> Repo.preload(:mejor_oferta)
render(conn, "show.json", subasta: subasta)
end
def create(conn, %{"subasta" => subasta_params}) do
changeset = Subasta.changeset(%Subasta{mejor_oferta: nil}, subasta_params)
case Repo.insert(changeset) do
{:ok, subasta} ->
conn
|> put_status(:created)
|> put_resp_header("location", subasta_path(conn, :show, subasta))
|> render("show.json", subasta: subasta)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(IascSubastas.ChangesetView, "error.json", changeset: changeset)
end
end
def update(conn, %{"id" => id, "subasta" => subasta_params}) do
subasta = Repo.get!(Subasta, id) |> Repo.preload(:mejor_oferta)
changeset = Subasta.changeset(subasta, subasta_params)
case Repo.update(changeset) do
{:ok, subasta} ->
render(conn, "show.json", subasta: subasta)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(IascSubastas.ChangesetView, "error.json", changeset: changeset)
end
end
def cancelar(conn, %{"subasta_id" => id}) do
subasta = Repo.get!(Subasta, id) |> Repo.preload(:mejor_oferta)
changeset = Subasta.changeset(subasta, %{terminada: true})
case Repo.update(changeset) do
{:ok, subasta} ->
render(conn, "show.json", subasta: subasta)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(IascSubastas.ChangesetView, "error.json", changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
subasta = Repo.get!(Subasta, id)
# Here we use delete! (with a bang) because we expect
# it to always work (and if it does not, it will raise).
Repo.delete!(subasta)
send_resp(conn, :no_content, "")
end
end
我只在 http://localhost:4000/api/subastas/ 出现错误,任何其他 URL 都可以正常工作.
I only get the error at http://localhost:4000/api/subastas/, any other URL works good.
推荐答案
POST 中缺少 content_type!应用程序/json"
content_type was missing in POST! "application/json"
这篇关于POST 时的 Phoenix.ActionClauseError,没有匹配的操作子句来处理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!