我正在从书Programming Phoenix
学习凤凰框架,并且遇到了一个我似乎无法理解的错误。基本上,本书目前正在添加一个用于创建新用户的表单。因此,当我将浏览器指向http://localhost:4000/users/new
时,我会看到以下页面(请展开图片):
在命令行输出中,服务器说:
[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
控制器:
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 %>
如果还需要其他一些代码,请告诉我。并提前感谢!
答案 0 :(得分:6)
~w
sigil在每个元素之后不需要逗号,但是当你添加它们时,它们会连接到列表项,所以在你的情况下
~w(name, username)
生成以下列表:
["name,", "username"]
这显然不是你想要的。
所以你应该删除逗号并改为使用它:
~w(name username)
产生了你期望的结果:
["name", "username"]