Val*_*llo 1 rest controller elixir
每当我发布到/ api/subastas时,我都会收到此错误
Phoenix.ActionClauseError at POST /api/subastas/ bad request to IascSubastas.SubastaController.create, no matching action clause to process request
Run Code Online (Sandbox Code Playgroud)
如果我运行mix phoenix.routes,我看到:create正确路由到POST/api/subastas
这是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
Run Code Online (Sandbox Code Playgroud)
和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
Run Code Online (Sandbox Code Playgroud)
我只在http:// localhost:4000/api/subastas /得到错误,任何其他URL工作正常.
| 归档时间: |
|
| 查看次数: |
3652 次 |
| 最近记录: |