在UserController上创建用户:create之后,我想将它们重定向到session_path(conn,:create)以让他们登录。但是我无法使呼叫正常工作。这就是我试过的:
def create(conn, %{"user" => user_params}) do
changeset = User.registration_changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, user} ->
conn
|> put_status(:created)
|> redirect(to: session_path(conn, :create, Poison.encode!(user_params)))
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(Prode.UserView, "error.json", errors: changeset.errors)
end
end
抛出以下错误:
(Protocol.UndefinedError) protocol Enumerable not implemented for "{\"password\":\"s3cr3t\",\"email\":\"foo@barr.com\"}"
在这一行:
|> redirect(to: session_path(conn, :create, Poison.encode!(user_params)))
我应该为User添加路由,并且会话控制器设置为使用json:
pipeline :api do
plug :accepts, ["json"]
end
scope "/api", Prode do
pipe_through :api
resources "/users", UserController
resources "/sessions", SessionController, only: [:create, :delete]
end
SessionController创建接受user_params:
def create(conn, %{"user" => user_params}) do
...
end