如何检查自动生成的Phoenix路径的参数

时间:2017-06-11 05:29:28

标签: phoenix-framework

  

Api.Router.Helpers.v1_user_organization_path没有辅助子句   为行动定义:显示与arity 3.请检查该功能,   arity和行动是正确的。以下是v1_user_organization_path   在您的路由器下定义了操作:
  *:创建
  *:指数
  *:显示
  *:更新

router.ex

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

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

  scope "/", Api do
    pipe_through :api

  end

  scope "/v1", Api.V1, as: :v1 do
    pipe_through :api

    resources "/users", UserController, only: [:create, :show, :update] do
      resources "/organizations", OrganizationController, only: [:create, :update, :index, :show]
    end
  end
end

当我混合phoenix.routes时,我看到生成了以下v1_user_organization_path。问题是我不知道如何使用它,我不知道应该把它传递给它。有没有办法可以检查这个生成的方法接受什么?

我得到的错误发生在这里

organization_controller.ex

def create(conn, %{"user_id" => user_id, "organization" => organization_params}) do
    changeset = Organization.changeset(%Organization{}, organization_params)

    case Repo.insert(changeset) do
      {:ok, organization} ->
        conn
        |> put_status(:created)
        |> put_resp_header("location", v1_user_organization_path(conn, :show, organization))
        |> render("show.json", organization: organization)
      {:error, changeset} ->
        conn
        |> put_status(:unprocessable_entity)
        |> render(Api.ChangesetView, "error.json", changeset: changeset)
    end
  end

put_resp_header("location", v1_user_organization_path(conn, :show, organization))

1 个答案:

答案 0 :(得分:1)

您可以使用h中的iex -S mix查看路由器帮助程序函数的自动生成文档,并阅读mix phoenix.routes的输出以获取一些帮助。例如,对于以下路线:

resources "/posts", PostController do
  resources "/comments", CommentController
end

我明白了:

iex(1)> h MyApp.Router.Helpers.post_comment_path
def post_comment_path(conn_or_endpoint, action, post_id)
def post_comment_path(conn_or_endpoint, action, post_id, params)
def post_comment_path(conn_or_endpoint, action, post_id, id, params)
$ mix phoenix.routes
post_comment_path  GET     /posts/:post_id/comments           MyApp.CommentController :index
post_comment_path  GET     /posts/:post_id/comments/:id/edit  MyApp.CommentController :edit
post_comment_path  GET     /posts/:post_id/comments/new       MyApp.CommentController :new
post_comment_path  GET     /posts/:post_id/comments/:id       MyApp.CommentController :show
post_comment_path  POST    /posts/:post_id/comments           MyApp.CommentController :create
post_comment_path  PATCH   /posts/:post_id/comments/:id       MyApp.CommentController :update
                   PUT     /posts/:post_id/comments/:id       MyApp.CommentController :update
post_comment_path  DELETE  /posts/:post_id/comments/:id       MyApp.CommentController :delete

仅从函数签名中不清楚哪个动作接受了多少个参数,但是如果您阅读mix phoenix.routes的输出,则可以看到:show(最后一列)需要{{1}和post_id

id的输出也不完全准确,因为它没有告诉您,arity 4版本也接受h而不只是(conn_or_endpoint, action, post_id, id)

我认为凤凰城目前生成的路线功能没有更好的自动生成文档。我通常只看(conn_or_endpoint, action, post_id, params)的输出并传入mix phoenix.routes后跟conn_or_endpoint后跟路线中的每个action,后面跟一个参数图。