Elixir 对Lxphnx.ArticleController.create的请求不正确,没有匹配的操作子句来处理请求
我可以做一个GET请求,但我似乎无法让POST请求工作 这是我的路线Elixir 对Lxphnx.ArticleController.create的请求不正确,没有匹配的操作子句来处理请求,elixir,phoenix-framework,Elixir,Phoenix Framework,我可以做一个GET请求,但我似乎无法让POST请求工作 这是我的路线 article_path GET /api/articles Lxphnx.ArticleController :index article_path GET /api/articles/:id Lxphnx.ArticleController :show article_path POST /api/articles Lxphnx.ArticleController :cre
article_path GET /api/articles Lxphnx.ArticleController :index
article_path GET /api/articles/:id Lxphnx.ArticleController :show
article_path POST /api/articles Lxphnx.ArticleController :create
article_path PATCH /api/articles/:id Lxphnx.ArticleController :update
PUT /api/articles/:id Lxphnx.ArticleController :update
article_path DELETE /api/articles/:id Lxphnx.ArticleController :delete
除了使用mix phoenix.gen.json之外,我什么都没碰过。这个项目只是一个API,所以我在创建项目时也使用了——没有早午餐——没有html
控制器:
defmodule Lxphnx.ArticleController do
use Lxphnx.Web, :controller
alias Lxphnx.Article
def index(conn, _params) do
articles = Repo.all(Article)
render(conn, "index.json", articles: articles)
end
def create(conn, %{"article" => article_params}) do
changeset = Article.changeset(%Article{}, article_params)
case Repo.insert(changeset) do
{:ok, article} ->
conn
|> put_status(:created)
|> put_resp_header("location", article_path(conn, :show, article))
|> render("show.json", article: article)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Lxphnx.ChangesetView, "error.json", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
article = Repo.get!(Article, id)
render(conn, "show.json", article: article)
end
def update(conn, %{"id" => id, "article" => article_params}) do
article = Repo.get!(Article, id)
changeset = Article.changeset(article, article_params)
case Repo.update(changeset) do
{:ok, article} ->
render(conn, "show.json", article: article)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Lxphnx.ChangesetView, "error.json", changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
article = Repo.get!(Article, 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!(article)
send_resp(conn, :no_content, "")
end
end
defmodule Lxphnx.ArticleView do
use Lxphnx.Web, :view
def render("index.json", %{articles: articles}) do
%{data: render_many(articles, Lxphnx.ArticleView, "article.json")}
end
def render("show.json", %{article: article}) do
%{data: render_one(article, Lxphnx.ArticleView, "article.json")}
end
def render("article.json", %{article: article}) do
%{id: article.id,
title: article.title,
body: article.body,
type: article.type}
end
end
路由器:
defmodule Lxphnx.ArticleController do
use Lxphnx.Web, :controller
alias Lxphnx.Article
def index(conn, _params) do
articles = Repo.all(Article)
render(conn, "index.json", articles: articles)
end
def create(conn, %{"article" => article_params}) do
changeset = Article.changeset(%Article{}, article_params)
case Repo.insert(changeset) do
{:ok, article} ->
conn
|> put_status(:created)
|> put_resp_header("location", article_path(conn, :show, article))
|> render("show.json", article: article)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Lxphnx.ChangesetView, "error.json", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
article = Repo.get!(Article, id)
render(conn, "show.json", article: article)
end
def update(conn, %{"id" => id, "article" => article_params}) do
article = Repo.get!(Article, id)
changeset = Article.changeset(article, article_params)
case Repo.update(changeset) do
{:ok, article} ->
render(conn, "show.json", article: article)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Lxphnx.ChangesetView, "error.json", changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
article = Repo.get!(Article, 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!(article)
send_resp(conn, :no_content, "")
end
end
defmodule Lxphnx.ArticleView do
use Lxphnx.Web, :view
def render("index.json", %{articles: articles}) do
%{data: render_many(articles, Lxphnx.ArticleView, "article.json")}
end
def render("show.json", %{article: article}) do
%{data: render_one(article, Lxphnx.ArticleView, "article.json")}
end
def render("article.json", %{article: article}) do
%{id: article.id,
title: article.title,
body: article.body,
type: article.type}
end
end
deffmodule Lxphnx.Router do
使用Lxphnx.Web,:路由器
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", Lxphnx do
pipe_through :api
resources "/articles", ArticleController, except: [:new, :edit]
end
end
查看:
defmodule Lxphnx.ArticleController do
use Lxphnx.Web, :controller
alias Lxphnx.Article
def index(conn, _params) do
articles = Repo.all(Article)
render(conn, "index.json", articles: articles)
end
def create(conn, %{"article" => article_params}) do
changeset = Article.changeset(%Article{}, article_params)
case Repo.insert(changeset) do
{:ok, article} ->
conn
|> put_status(:created)
|> put_resp_header("location", article_path(conn, :show, article))
|> render("show.json", article: article)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Lxphnx.ChangesetView, "error.json", changeset: changeset)
end
end
def show(conn, %{"id" => id}) do
article = Repo.get!(Article, id)
render(conn, "show.json", article: article)
end
def update(conn, %{"id" => id, "article" => article_params}) do
article = Repo.get!(Article, id)
changeset = Article.changeset(article, article_params)
case Repo.update(changeset) do
{:ok, article} ->
render(conn, "show.json", article: article)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Lxphnx.ChangesetView, "error.json", changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
article = Repo.get!(Article, 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!(article)
send_resp(conn, :no_content, "")
end
end
defmodule Lxphnx.ArticleView do
use Lxphnx.Web, :view
def render("index.json", %{articles: articles}) do
%{data: render_many(articles, Lxphnx.ArticleView, "article.json")}
end
def render("show.json", %{article: article}) do
%{data: render_one(article, Lxphnx.ArticleView, "article.json")}
end
def render("article.json", %{article: article}) do
%{id: article.id,
title: article.title,
body: article.body,
type: article.type}
end
end
哦,我也试过这个
我使用的是cURL,我没有忘记它是一个application/json。这是我的curl请求:
curl-X POST-d'{“id”:1,“title”:“a title”,“body”:“a body”,“type:1”}'-o log.txt localhost:4000/api/articles
如果我使用postman,我会得到相同的结果。为了使函数子句匹配,需要在curl请求中指定“article”:
curl -X POST -d '{"article": {"id":1, "title":"a title", "body":"a body", "type:1"}}' -o log.txt localhost:4000/api/articles
如果不希望对象中存在此顶级关键点,请更改:
def create(conn, %{"article" => article_params}) do
changeset = Article.changeset(%Article{}, article_params)
致:
为了使function子句匹配,您需要在curl请求中指定“article”:
curl -X POST -d '{"article": {"id":1, "title":"a title", "body":"a body", "type:1"}}' -o log.txt localhost:4000/api/articles
如果不希望对象中存在此顶级关键点,请更改:
def create(conn, %{"article" => article_params}) do
changeset = Article.changeset(%Article{}, article_params)
致:
您正在尝试模式匹配“article”,但遗憾的是您没有在有效负载中设置article;)@JeremieGes抱歉,您能详细说明一下您的意思吗?您正在尝试模式匹配“文章”,但遗憾的是您没有在有效负载中设置文章;)@JeremieGes对不起,你能详细解释一下你的意思吗?我正要自己写答案。谢谢:)我正要自己写答案。谢谢:)