我有这个小型控制器来处理用户注册
def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
IO.inspect(user_params)
errors = []
cond do
user_params["name"] === nil -> errors = ["Name cannot be empty" | errors ]
String.length(user_params["name"]) >= 50 -> errors = ["Name cannot be longer than 50 characters" | errors ]
String.length(user_params["name"]) <= 3 -> errors = ["Name cannot be shorter than 3 characters" | errors ]
end
cond do
user_params["email"] === nil -> errors = ["Email cannot be empty" | errors ]
String.length(user_params["email"]) >= 50 -> errors = ["Email cannot be longer than 50 characters" | errors ]
String.length(user_params["email"]) <= 5 -> errors = ["Email cannot be shorter than 3 characters" | errors ]
end
cond do
user_params["username"] === nil -> errors = ["Username cannot be empty" | errors ]
String.length(user_params["username"]) >= 30 -> errors = ["Username cannot be longer than 30 characters" | errors ]
String.length(user_params["username"]) <= 3 -> errors = ["Username cannot be shorter than 3 characters" | errors ]
end
cond do
user_params["password"] === nil -> errors = ["Password cannot be empty" | errors ]
user_params["passwordC"] === nil -> errors = ["Password Confirmation cannot be empty" | errors ]
String.length(user_params["password"]) >= 30 -> errors = ["Password cannot be longer than 30 characters" | errors ]
String.length(user_params["password"]) <= 3 -> errors = ["Password cannot be shorter than 3 characters" | errors ]
user_params["password"] !== user_params["passwordC"] -> errors = ["Passwords do not match" | errors ]
end
cond do
(length errors) >= 1 -> conn |> json %{success: false, errors: errors}
(length errors) == 0 -> conn |> json %{success: true}
end
end
我得到了json的回复,但我一直在提出这个异常:
Request: POST /auth/register
** (exit) an exception was raised:
** (RuntimeError) expected action/2 to return a Plug.Conn, all plugs must receive a connection (conn) and return a connection
(bdo_pug) web/controllers/user_controller.ex:1: BdoPug.UserController.phoenix_controller_pipeline/2
(bdo_pug) lib/phoenix/router.ex:261: BdoPug.Router.dispatch/2
(bdo_pug) web/router.ex:1: BdoPug.Router.do_call/2
(bdo_pug) lib/bdo_pug/endpoint.ex:1: BdoPug.Endpoint.phoenix_pipeline/1
(bdo_pug) lib/plug/debugger.ex:93: BdoPug.Endpoint."call (overridable 3)"/2
(bdo_pug) lib/phoenix/endpoint/render_errors.ex:34: BdoPug.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
我已经用Google搜索了它并且它一直告诉我我需要返回我相信我的conn
。任何信息都会非常感谢。
defmodule BdoPug.UserController do
use BdoPug.Web, :controller
alias BdoPug.User
plug :scrub_params, "user" when action in [:create, :update]
def new(conn, _params) do
changeset = User.changeset(%User{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
errors = []
cond do
user_params["name"] === nil -> errors = ["Name cannot be empty" | errors ]
String.length(user_params["name"]) >= 50 -> errors = ["Name cannot be longer than 50 characters" | errors ]
String.length(user_params["name"]) <= 3 -> errors = ["Name cannot be shorter than 3 characters" | errors ]
end
cond do
user_params["email"] === nil -> errors = ["Email cannot be empty" | errors ]
String.length(user_params["email"]) >= 50 -> errors = ["Email cannot be longer than 50 characters" | errors ]
String.length(user_params["email"]) <= 5 -> errors = ["Email cannot be shorter than 3 characters" | errors ]
end
cond do
user_params["username"] === nil -> errors = ["Username cannot be empty" | errors ]
String.length(user_params["username"]) >= 30 -> errors = ["Username cannot be longer than 30 characters" | errors ]
String.length(user_params["username"]) <= 3 -> errors = ["Username cannot be shorter than 3 characters" | errors ]
end
cond do
user_params["password"] === nil -> errors = ["Password cannot be empty" | errors ]
user_params["passwordC"] === nil -> errors = ["Password Confirmation cannot be empty" | errors ]
String.length(user_params["password"]) >= 30 -> errors = ["Password cannot be longer than 30 characters" | errors ]
String.length(user_params["password"]) <= 3 -> errors = ["Password cannot be shorter than 3 characters" | errors ]
user_params["password"] !== user_params["passwordC"] -> errors = ["Passwords do not match" | errors ]
end
cond do
(length errors) >= 1 -> conn |> json %{success: false, errors: errors}
(length errors) == 0 -> conn |> json %{success: true}
end
"""
case Repo.insert(changeset) do
{:ok, _user} ->
conn
|> put_flash(:info, "User created successfully.")
|> redirect(to: user_path(conn, :index))
{:error, changeset} ->
render(conn, "new.html", changeset: changeset)
end
"""
end
def loginView(conn, _params) do
render(conn, "loginView.html")
end
def loginPost(conn, %{"user" => user_params}) do
end
end
答案 0 :(得分:1)
好像你在case
中包含了自动生成的"""
语句。在Elixir中,"""
是HEREDOC的开始,所以你基本上在函数的末尾创建了一个大字符串。 Elixir函数返回函数体中的最后一个值,该值是原始值或函数的结果。因此,它不是返回调用函数所期望的Plug.Conn
结构,而是返回HEREDOC的字符串,因为这是最后一个值。