使用“从插头分配到视图”(elixir/phoenix)
我有我的api管道,我想在其中添加一个插件,该插件基本上在数据库中执行一些操作,并根据响应执行分配,然后在视图中使用该分配 我在将分配从插件传递到视图时遇到问题 web/router.ex使用“从插头分配到视图”(elixir/phoenix),elixir,phoenix-framework,plug,Elixir,Phoenix Framework,Plug,我有我的api管道,我想在其中添加一个插件,该插件基本上在数据库中执行一些操作,并根据响应执行分配,然后在视图中使用该分配 我在将分配从插件传递到视图时遇到问题 web/router.ex pipeline :api do plug ApiV2.Plug.Authenticate, repo: Auth.Repo plug :accepts, ["json"] end defmodule ApiV2.AddressController do use ApiV2.Web, :cont
pipeline :api do
plug ApiV2.Plug.Authenticate, repo: Auth.Repo
plug :accepts, ["json"]
end
defmodule ApiV2.AddressController do
use ApiV2.Web, :controller
alias ApiV2.Address
def index(conn, params) do
json conn, @current_user
end
end
web/controllers/auth.ex(仅分配)
web/controllers/address\u controller.ex
pipeline :api do
plug ApiV2.Plug.Authenticate, repo: Auth.Repo
plug :accepts, ["json"]
end
defmodule ApiV2.AddressController do
use ApiV2.Web, :controller
alias ApiV2.Address
def index(conn, params) do
json conn, @current_user
end
end
我已经到达了它试图返回的点:当前用户(我想),但是,由于赋值错误,它只返回
null
我错过了什么
谢谢使用
assign
设置的值可在conn.assigns
映射中找到@current_user
是一个模块属性,它返回nil
,因为它未在此模块中设置
这应该起作用:
json conn, conn.assigns.current_user