2016-07-22 9 views
1

Ich kann eine GET-Anfrage machen, aber ich kann nicht scheinen, dass eine POST-Anfrage funktioniert.schlechte Anfrage an Lxphnx.ArticleController.create, keine passende Aktionsklausel zur Bearbeitung der Anfrage

Das sind meine Routen

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 

Ich habe nicht wirklich etwas berührt, außer für Mischung phoenix.gen.json verwenden. Dieses Projekt ist nur eine API, also habe ich beim Erstellen des Projekts auch --no-brunch --no-html verwendet.

Controller:

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 

Router:

defmodule Lxphnx.Router tun Verwendung 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 

Ausblick:

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 

Oh, und ich habe auch versucht, diese Phoenix.ActionClauseError at POST, no matching action clause to process request

Ich verwende Curl und ich vergesse nicht zu setzen, dass es ein application/json. Hier ist meine Curl Anfrage: curl -X POST -d '{"id":1, "title":"a title", "body":"a body", "type:1"}' -o log.txt localhost:4000/api/articles Ich bekomme die gleichen Ergebnisse, wenn ich Briefträger benutze.

+0

Sie versuchen, auf Mustererkennung "Artikel", aber bummer Sie keine Artikel in Ihrer Nutzlast gesetzt;) –

+0

@ JeremieGes Entschuldigung, aber könnten Sie näher erläutern, was Sie meinen? –

Antwort

6

Damit Ihre Funktion Klausel entsprechen, müssen Sie „Artikel“ in Ihrem curl Anfrage spezifizieren:

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

Wenn Sie diese Top-Level-Schlüssel in Ihrem Objekt, ändern wollen:

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

zu:

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

Ich wollte gerade die Antwort selbst schreiben. Vielen Dank :) –