gpt4 book ai didi

elixir - 变更集中的参数错误

转载 作者:行者123 更新时间:2023-12-02 08:19:11 24 4
gpt4 key购买 nike

我正在从 Programming Phoenix 一书中学习 Phoenix 框架,但遇到了一个我似乎无法理解的错误。基本上,这本书目前正在添加一个用于创建新用户的表单。因此,当我将浏览器指向 http://localhost:4000/users/new 时,我得到以下页面(请展开图片):

enter image description here

在命令行输出中服务器说:

[info] GET /users/new
[debug] Processing by Rumbl.UserController.new/2
Parameters: %{}
Pipelines: [:browser]
[info] Sent 500 in 9ms
[error] #PID<0.543.0> running Rumbl.Endpoint terminated
Server: localhost:4000 (http)
Request: GET /users/new
** (exit) an exception was raised:
** (ArgumentError) argument error
:erlang.binary_to_existing_atom("name,", :utf8)
(ecto) lib/ecto/changeset.ex:402: Ecto.Changeset.process_empty_fields/2
(elixir) lib/enum.ex:1184: Enum."-map/2-lists^map/1-0-"/2
(ecto) lib/ecto/changeset.ex:373: Ecto.Changeset.do_cast/7
(rumbl) web/models/user.ex:15: Rumbl.User.changeset/2
(rumbl) web/controllers/user_controller.ex:17: Rumbl.UserController.new/2
(rumbl) web/controllers/user_controller.ex:1: Rumbl.UserController.action/2
(rumbl) web/controllers/user_controller.ex:1: Rumbl.UserController.phoenix_controller_pipeline/2
(rumbl) lib/rumbl/endpoint.ex:1: Rumbl.Endpoint.instrument/4
(rumbl) lib/phoenix/router.ex:261: Rumbl.Router.dispatch/2
(rumbl) web/router.ex:1: Rumbl.Router.do_call/2
(rumbl) lib/rumbl/endpoint.ex:1: Rumbl.Endpoint.phoenix_pipeline/1
(rumbl) lib/plug/debugger.ex:122: Rumbl.Endpoint."call (overridable 3)"/2
(rumbl) lib/rumbl/endpoint.ex:1: Rumbl.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

由于错误显示类似 binary_to_existing_atom 之类的内容,我想我可能输入了错误的参数或其他内容,但事实并非如此。

我的模型文件:

defmodule Rumbl.User do
use Rumbl.Web, :model

schema "users" do
field :name, :string
field :username, :string
field :password, :string, virtual: true
field :password_hash, :string

timestamps
end

def changeset(model, params \\ :invalid) do
model
|> cast(params, ~w(name, username), [])
|> validate_length(:username, min: 1, max: 20)
end
end

我的迁移文件:

defmodule Rumbl.Repo.Migrations.CreateUser do
use Ecto.Migration

def change do
create table(:users) do
add :name, :string
add :username, :string, null: false
add :password_hash, :string

timestamps
end

create unique_index(:users, [:username])
end
end

Controller :

defmodule Rumbl.UserController do
use Rumbl.Web, :controller

def index(conn, _params) do
users = Repo.all(Rumbl.User)
render conn, "index.html", users: users
end

def show(conn, %{"id" => id}) do
user = Repo.get(Rumbl.User, id)
render conn, "show.html", user: user
end

alias Rumbl.User

def new(conn, _params) do
changeset = User.changeset(%User{})
render conn, "new.html", changeset: changeset
end
end

最后,模板:

<h1>New User</h1>

<%= form_for @changeset, user_path(@conn, :create), fn f -> %>
<div class="form-group">
<%= text_input f, :name, placeholder: "Username", class: "form-control" %>
</div>
<div class="form-group">
<%= password_input f, :password, placeholder: "Password", class: "form-control" %>
</div>
<%= submit "Create User", class: "btn btn-primary" %>
<% end %>

如果还需要一些其他代码,请告诉我。提前致谢!

最佳答案

~w sigil 不需要在每个元素后使用逗号,但是当您添加这些元素时,它们会连接到列表项,因此在您的情况下

~w(name, username)

生成了以下列表:

["name,", "username"]

这显然不是你想要的。

所以你应该只删除逗号并改用它:

~w(name username)

它生成的正是您所期望的:

["name", "username"]

关于elixir - 变更集中的参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38943811/

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