JSON 的 Phoenix 框架 router.ex 配置
Phoenix framework router.ex config for JSON
我已经通过 mix phoenix.gen.json V1.Post posts title:string content:string secret:string --no-model
安装了 phoenix JSON 软件包
但出现此错误:
== Compilation error on file web/controllers/v1/post_controller.ex ==
** (CompileError) web/controllers/v1/post_controller.ex:14: Shopper.V1.Post.__struct__/0 is undefined, cannot expand struct Shopper.V1.Post
(elixir) src/elixir_map.erl:55: :elixir_map.translate_struct/4
(stdlib) lists.erl:1353: :lists.mapfoldl/3
(elixir) src/elixir_clauses.erl:36: :elixir_clauses.clause/7
(elixir) src/elixir_def.erl:178: :elixir_def.translate_clause/7
(elixir) src/elixir_def.erl:167: :elixir_def.translate_definition/8
(elixir) src/elixir_def.erl:82: :elixir_def.store_definition/9
web/controllers/v1/post_controller.ex:13: (module)
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
这是我的 router.ex 代码:
defmodule Shopper.Router do
use Shopper.Web, :router
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Shopper do
pipe_through :api
resources "/v1/posts", V1.PostController
end
end
从文档中说:
Add the resource to the proper scope in web/router.ex:
resources "/posts", PostController
但是..我做不到,有人能帮帮我吗?谢谢。
请注意,这是针对 phoenix 1.0 和 elixir 1.0 的。
我的问题指向错误的问题,但我会保持原样,这样可以帮助得出类似结论的人。
解决方案是为 V1.Post 添加一个模型,但是 运行ning mix phoenix.gen.model
会给出一个编译错误,我的问题中已经说明了这一点。因此,我必须评论 V1.Post 控制器中整个 def create(conn, %{"post" => post_params})
的违规代码。
之后 运行 mix phoenix.gen.model V1.Post posts name:string
然后取消注释有问题的代码。
为了测试,运行 mix phoenix.routes
在我的例子中返回
Compiled web/models/v1/post.ex
Generated shopper app
page_path GET / Shopper.PageController :index
post_path GET /api/v1/posts Shopper.V1.PostController :index
post_path GET /api/v1/posts/:id Shopper.V1.PostController :show
post_path POST /api/v1/posts Shopper.V1.PostController :create
post_path PATCH /api/v1/posts/:id Shopper.V1.PostController :update
PUT /api/v1/posts/:id Shopper.V1.PostController :update
post_path DELETE /api/v1/posts/:id Shopper.V1.PostController :delete
成功!
我已经通过 mix phoenix.gen.json V1.Post posts title:string content:string secret:string --no-model
安装了 phoenix JSON 软件包
但出现此错误:
== Compilation error on file web/controllers/v1/post_controller.ex ==
** (CompileError) web/controllers/v1/post_controller.ex:14: Shopper.V1.Post.__struct__/0 is undefined, cannot expand struct Shopper.V1.Post
(elixir) src/elixir_map.erl:55: :elixir_map.translate_struct/4
(stdlib) lists.erl:1353: :lists.mapfoldl/3
(elixir) src/elixir_clauses.erl:36: :elixir_clauses.clause/7
(elixir) src/elixir_def.erl:178: :elixir_def.translate_clause/7
(elixir) src/elixir_def.erl:167: :elixir_def.translate_definition/8
(elixir) src/elixir_def.erl:82: :elixir_def.store_definition/9
web/controllers/v1/post_controller.ex:13: (module)
(stdlib) erl_eval.erl:669: :erl_eval.do_apply/6
这是我的 router.ex 代码:
defmodule Shopper.Router do
use Shopper.Web, :router
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Shopper do
pipe_through :api
resources "/v1/posts", V1.PostController
end
end
从文档中说:
Add the resource to the proper scope in web/router.ex:
resources "/posts", PostController
但是..我做不到,有人能帮帮我吗?谢谢。
请注意,这是针对 phoenix 1.0 和 elixir 1.0 的。
我的问题指向错误的问题,但我会保持原样,这样可以帮助得出类似结论的人。
解决方案是为 V1.Post 添加一个模型,但是 运行ning mix phoenix.gen.model
会给出一个编译错误,我的问题中已经说明了这一点。因此,我必须评论 V1.Post 控制器中整个 def create(conn, %{"post" => post_params})
的违规代码。
之后 运行 mix phoenix.gen.model V1.Post posts name:string
然后取消注释有问题的代码。
为了测试,运行 mix phoenix.routes
在我的例子中返回
Compiled web/models/v1/post.ex
Generated shopper app
page_path GET / Shopper.PageController :index
post_path GET /api/v1/posts Shopper.V1.PostController :index
post_path GET /api/v1/posts/:id Shopper.V1.PostController :show
post_path POST /api/v1/posts Shopper.V1.PostController :create
post_path PATCH /api/v1/posts/:id Shopper.V1.PostController :update
PUT /api/v1/posts/:id Shopper.V1.PostController :update
post_path DELETE /api/v1/posts/:id Shopper.V1.PostController :delete
成功!