gpt4 book ai didi

elixir - 无法在 Phoenix 中创建嵌套资源

转载 作者:行者123 更新时间:2023-12-02 03:41:50 26 4
gpt4 key购买 nike

我正在尝试创建一个按钮,使给定用户成为管理员。为此,我想为请求 post/users/:id/admin 创建一条路由。为了做到这一点,我尝试创建一个嵌套资源,如下所示:

resources "/users", UserController, only: [:new, :create, :index] do
resources "/admin", UserController, only: [:post]
end

但是当我运行 mix phx.routes | grep 用户,我只获取这些路由:

user_path  GET     /users            StorexWeb.UserController :index
user_path GET /users/new StorexWeb.UserController :new
user_path POST /users StorexWeb.UserController :create

就好像未声明嵌套资源一样。我的资源声明有什么问题?我该如何修复它?

最佳答案

问题位于 only: [:post] 。没有这样的 Action :post ,所以你最终一无所获。您可能想要这个:

resources "/users", UserController, only: [:new, :create, :index] do
resources "/admin", UserController, only: [:create]
end

我将在 Phoenix 提出一个问题,针对这些情况提出以避免进一步困惑。

关于elixir - 无法在 Phoenix 中创建嵌套资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48358986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com