对Lxphnx.ArticleController.create的错误请求,没有匹配的操作子句来处理请求 [英] bad request to Lxphnx.ArticleController.create, no matching action clause to process request

查看:47
本文介绍了对Lxphnx.ArticleController.create的错误请求,没有匹配的操作子句来处理请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以执行GET请求,但似乎无法执行POST请求.

I can do a GET request but I can't seem to get a POST request working.

这是我的路线

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,因此在创建项目时也使用了--no-brunch --no-html.

I haven't really touched anything except for using mix phoenix.gen.json. This project is an API only so I also used --no-brunch --no-html when creating the project.

控制器:

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.Router做 使用Lxphnx.Web,:router

defmodule Lxphnx.Router do use Lxphnx.Web, :router

 pipeline :api do
   plug :accepts, ["json"]
 end

 scope "/api", Lxphnx do
   pipe_through :api
   resources "/articles", ArticleController, except: [:new, :edit]
 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

哦,我也尝试过 Phoenix POST时出现.ActionClauseError,没有匹配的操作子句来处理请求

我正在使用cURL,并且我没有忘记说它是一个应用程序/json.这是我的curl请求:curl -X POST -d '{"id":1, "title":"a title", "body":"a body", "type:1"}' -o log.txt localhost:4000/api/articles如果我使用邮递员,我将得到相同的结果.

I'm using cURL and I'm not forgetting to put that it's an application/json. Here is my curl request: curl -X POST -d '{"id":1, "title":"a title", "body":"a body", "type:1"}' -o log.txt localhost:4000/api/articles I get the same results if I use postman.

推荐答案

为了使您的function子句匹配,您需要在curl请求中指定文章":

In order for your function clause to match, you need to specify "article" in your curl request:

curl -X POST -d '{"article": {"id":1, "title":"a title", "body":"a body", "type:1"}}' -o log.txt localhost:4000/api/articles

如果您不想在对象中使用此顶级键,请更改:

If you don't want this top level key in your object, change:

def create(conn, %{"article" => article_params}) do
  changeset = Article.changeset(%Article{}, article_params)

收件人:

def create(conn, article_params) do
  changeset = Article.changeset(%Article{}, article_params)

这篇关于对Lxphnx.ArticleController.create的错误请求,没有匹配的操作子句来处理请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆