Elixir Can';t在phoenix中创建自定义链接:未实现协议枚举
我是Elixir/Phoenix新手,我想创建一个不使用模型ID但最终出现错误的链接:Elixir Can';t在phoenix中创建自定义链接:未实现协议枚举,elixir,phoenix-framework,Elixir,Phoenix Framework,我是Elixir/Phoenix新手,我想创建一个不使用模型ID但最终出现错误的链接: 未为%XX.YY.文章{{uuuu meta_uuuu:#exto.Schema.Metadata实现协议枚举,内容:nil,id:nil,inserted_at:nil,short_title:“lalalalillaa”,slug:457,title:nil,updated_at:nil}。 以下是我尝试创建链接的方式: def link_to_article(conn, article_map) d
未为%XX.YY.文章{{uuuu meta_uuuu:#exto.Schema.Metadata实现协议枚举,内容:nil,id:nil,inserted_at:nil,short_title:“lalalalillaa”,slug:457,title:nil,updated_at:nil}。
以下是我尝试创建链接的方式:
def link_to_article(conn, article_map) do
article = struct(%XX.YY.Article{}, article_map)
link article_map.short_title,
to: page_path(conn, :index, article),
class: "btn btn-default"
end
我为to_param提供了以下实现:
defimpl Phoenix.Param, for: XX.YY.Article do
alias XX.YY.Slug
def to_param(%{slug: slug, title: title}) do
"#{Slug.pad_slug(slug)}-#{Slug.slugify_title(title)}"
end
end
如果我将article\u map
提供给链接,那么该链接可以工作,但很难看(该映射将成为查询字符串)。对于文章
强制使用参数
并创建漂亮的URL,我缺少什么
编辑,以下是路由器声明:
get "/", PageController, :index
get "/:article", PageController, :index
因为我希望URL像qweqwe.com/padded\u slug-article\u title
在控制器中,我将索引定义为:
def index(conn, %{"article" => article_identifier}) do
[slug|_] = article_identifier |> String.split("-")
article = XX.YY.get_current_article(slug)
case article do
nil -> Phoenix.Controller.redirect(conn, to: "/")
article ->
render conn, "index.html",
articles: XX.YY.get_articles_for_menu!(),
current_article: article
end
end
def index(conn, _params) do
render conn, "index.html",
articles: XX.YY.get_articles_for_menu!(),
current_article: XX.YY.get_default_article!()
end
这代码难看吗
迄今为止的所有路线:
page_path GET / QQ.PageController :index
user_path GET /users QQ.UserController :index
user_path GET /users/:id/edit QQ.UserController :edit
user_path GET /users/new QQ.UserController :new
user_path GET /users/:id QQ.UserController :show
user_path POST /users QQ.UserController :create
user_path PATCH /users/:id QQ.UserController :update
PUT /users/:id QQ.UserController :update
user_path DELETE /users/:id QQ.UserController :delete
article_path GET /articles QQ.ArticleController :index
article_path GET /articles/:id/edit QQ.ArticleController :edit
article_path GET /articles/new QQ.ArticleController :new
article_path GET /articles/:id QQ.ArticleController :show
article_path POST /articles QQ.ArticleController :create
article_path PATCH /articles/:id QQ.ArticleController :update
PUT /articles/:id QQ.ArticleController :update
article_path DELETE /articles/:id QQ.ArticleController :delete
page_path GET /:article QQ.PageController :index
调用堆栈:
(elixir) lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) lib/enum.ex:116: Enumerable.reduce/3
(elixir) lib/enum.ex:1823: Enum.reduce/3
(XX) lib/QQ/router.ex:1: QQ.Router.Helpers.segments/3
(XX) lib/QQ/router.ex:1: QQ.Router.Helpers.page_path/3
(XX) lib/QQ/views/layout_view.ex:8: QQ.LayoutView.link_to_article/2
您有两条路由指向同一控制器/功能(
PageController.index
)。router helper函数仅为控制器/函数的第一个实例生成,这就是为什么如果尝试将项目作为第三个参数传递,会出现错误
如果您没有使用get/,PageController,:index
路由,删除该路由将修复错误
如果正在使用它,则必须向第二条管线添加自定义名称,如下所示:
get "/:article", PageController, :index, as: :article_index
现在,您可以使用article\u index\u path
生成路径:
article_index_path(conn, :index, article)
通常,
索引
路径不需要对象id或任何对象相关数据,因此最后一个参数只是URL参数的映射或关键字列表,但这是我的猜测,请使用显示
或编辑
路径进行测试。你能发布你的路由器.ex
吗?@Dogbert请查看我的编辑@这是因为我不希望URL中包含show
。我保留生成的资源用于管理目的,并使用自定义路由进行查看。@Shautieh您的路由器.ex
中是否有指向PageController.index
的其他路由?你能发布mix phoenix.routes的输出吗?哦,好的,我想我可以在这些路线上进行模式匹配,在我开始摆弄to_param和所有东西之前,它就已经起作用了。我以后会这么做的,如果有效,我会接受你的答案!感谢幸运的是,这并没有解决问题(我删除了:article
路线并保留了/
)。当我转到/articles
时(不是/users
),我也会出现完全相同的错误,因此我认为问题来自将articles
作为参数创建链接,从而来自to_param
函数。。??我将在问题中添加调用堆栈。您是否完全删除了该路由或使用我在回答中所写的as:
更改了其名称?根据stacktrace,您仍在使用page\u path
生成文章路线。你必须使用答案中提到的新名称。我完全删除了/:article
一个,因此唯一的路径page\u path
是page\u path GET/QQ.PageController:index
。正如我所说,当我进入文章页面请求:GET/articles
时,我有完全相同的错误,因为视图中的@articles
充满了articles
,在构建链接时它会断开。你必须添加其他路径,并使用它来生成链接,就像我在回答中所写的那样。您不能将文章传递给GET/
路线。