Elixir 获取错误exto.Changeset.fetch/2未定义或是私有的
我正在尝试创建一个简单的登录页面。每当我尝试将我的页面路由到用户注册时,我都会遇到以下错误Elixir 获取错误exto.Changeset.fetch/2未定义或是私有的,elixir,phoenix-framework,Elixir,Phoenix Framework,我正在尝试创建一个简单的登录页面。每当我尝试将我的页面路由到用户注册时,我都会遇到以下错误 UndefinedFunctionError at GET /users/new function Ecto.Changeset.fetch/2 is undefined or private. Did you mean one of: * fetch_change/2 * fetch_field/2 这个错误似乎来自变更集,因为每当我注释掉我的变更集并只呈现一个空的html页
UndefinedFunctionError at GET /users/new
function Ecto.Changeset.fetch/2 is undefined or private. Did you mean one of:
* fetch_change/2
* fetch_field/2
这个错误似乎来自变更集,因为每当我注释掉我的变更集并只呈现一个空的html页面时,它就会工作。任何帮助都将不胜感激
web/controllers/user_controller.ex
defmodule Test.UserController do
use Test.Web, :controller
alias Test.User
def new(conn, params) do
changeset = User.changeset(%User{})
render conn, "new.html", changeset
end
def show(conn, %{"user_id_hash" => user_id_hash}) do
render conn, "user_index.html", %{layout: {Test.LayoutView, "react.html"}}
end
def create(conn, %{"user" => user_params}) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "#{user.username} thank you for signing up please check your email to confirm your account.")
|> redirect(to: page_path(conn, :index))
{:error, changeset}->
render(conn, "new.html", changeset: changeset)
end
end
end
然后在web/models/user.ex中显示我的模型
defmodule Test.User do
use Test.Web, :model
#Allows us to query the Database as Repo.
alias Test.Repo
#Allows us to use our User model as User.(function)
alias Test.User
#These are
@fields ~w(username email password encrypted_password user_id_hash password_confirmation)a
#These are the required fields as atoms for the validate_required method.
@required_fields_atoms ~w(username email password password_confirmation)a
schema "users" do
field :username, :string
field :email, :string
field :encrypted_password, :string
field :user_id_hash, :string
field :account_confirmed, :boolean
field :confirmation_hash, :string
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @fields)
|> validate_required(@required_fields)
|> validate_length(:username, min: 6, max: 32)
|> validate_length(:password, min: 8, max: 100)
|> validate_confirmation(:password, message: "does not match password")
|> put_pass_hash()
end
def generate_confirmation_token() do
rand_string = random_string(26)
end
defp put_pass_hash(changeset) do
case changeset do
%Ecto.Changeset{valid?: true, changes: %{password: pass}} ->
put_change(changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(pass))
_ -> changeset
end
end
defp random_string(length) do
:crypto.strong_rand_bytes(length) |> Base.url_encode64 |> binary_part(0, length)
end
end
当运行程序时,错误表明它来自
Test.UserController.action/2 (test)
web/controllers/user_controller.ex (line 1)
我从来没有调用过fetch函数,如果有人能帮忙,我会非常感激
编辑添加堆栈跟踪
[error] #PID<0.365.0> running Test.Endpoint terminated
Server: my-name.c9users.io:8081 (http)
Request: GET /users/new
** (exit) an exception was raised:
** (UndefinedFunctionError) function Ecto.Changeset.fetch/2 is undefined or private. Did you mean one of:
* fetch_change/2
* fetch_field/2
(ecto) Ecto.Changeset.fetch(#Ecto.Changeset<action: nil, changes: %{}, errors: [username: {"can't be blank", []}], data: #Test.User<>, valid?: false>, :conn)
(phoenix_html) lib/phoenix_html/engine.ex:98: Phoenix.HTML.Engine.fetch_assign/2
(test) web/templates/layout/app.html.eex:11: Test.LayoutView."app.html"/1
(phoenix) lib/phoenix/view.ex:335: Phoenix.View.render_to_iodata/3
(phoenix) lib/phoenix/controller.ex:641: Phoenix.Controller.do_render/4
(test) web/controllers/user_controller.ex:1: Test.UserController.action/2
(test) web/controllers/user_controller.ex:1: Test.UserController.phoenix_controller_pipeline/2
(test) lib/test/endpoint.ex:1: Test.Endpoint.instrument/4
(test) lib/phoenix/router.ex:261: Test.Router.dispatch/2
(test) web/router.ex:1: Test.Router.do_call/2
(test) lib/test/endpoint.ex:1: Test.Endpoint.phoenix_pipeline/1
(test) lib/plug/debugger.ex:93: Test.Endpoint."call (overridable 3)"/2
(test) lib/test/endpoint.ex:1: Test.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
^C
[错误]#PID正在运行测试。终结点已终止
服务器:my name.c9users.io:8081(http)
请求:获取/用户/新建
**(退出)引发了一个异常:
**(UndefinedFunctionError)函数exto.Changeset.fetch/2未定义或私有。你的意思是:
*取零钱/2
*取数字段/2
(外部)外部变更集获取(#外部变更集,:conn)
(phoenix_html)lib/phoenix_html/engine.ex:98:phoenix.html.engine.fetch_assign/2
(test)web/templates/layout/app.html.eex:11:test.LayoutView.“app.html”/1
(phoenix)lib/phoenix/view.ex:335:phoenix.view.render_to_iodata/3
(phoenix)lib/phoenix/controller.ex:641:phoenix.controller.do_render/4
(test)web/controllers/user\u controller.ex:1:test.UserController.action/2
(test)web/controllers/user\u controller.ex:1:test.UserController.phoenix\u controller\u管道/2
(test)lib/test/endpoint.ex:1:test.endpoint.instrument/4
(test)lib/phoenix/router.ex:261:test.router.dispatch/2
(测试)web/router.ex:1:test.router.do_call/2
(test)lib/test/endpoint.ex:1:test.endpoint.phoenix_管道/1
(test)lib/plug/debugger.ex:93:test.Endpoint.“调用(可重写3)”/2
(test)lib/test/endpoint.ex:1:test.endpoint.call/2
(plug)lib/plug/adapters/cowboy/handler.ex:15:plug.adapters.cowboy.handler.upgrade/4
(牛仔)src/cowboy_u协议。erl:442::cowboy\u protocol.execute/4
^C
在测试.UserController
中,这:
render conn, "new.html", changeset
应该是
render conn, "new.html", changeset: changeset
你得到的奇怪错误是因为。在这种情况下,您很可能在模板中的某个地方使用了
@conn
,它试图运行Dict.fetch(changeset,:conn)
,它将委托给changeset.\uu struct\uu.fetch(changeset,:conn)
作为changeset
结构,并最终调用exto.changeset.fetch(changeset,:conn)
在测试.UserController
中:
render conn, "new.html", changeset
应该是
render conn, "new.html", changeset: changeset
你得到的奇怪错误是因为。在这种情况下,您很可能在模板中的某个地方使用了
@conn
,它试图运行Dict.fetch(changeset,:conn)
,它将委托给changeset.\uu struct\uu.fetch(changeset,:conn)
作为changeset
结构,并最终调用exto.changeset.fetch(changeset,:conn)
您能将Elixir打印的确切堆栈跟踪添加到问题中吗?@Dogbert,我已使用堆栈跟踪更新了问题。您能将Elixir打印的确切堆栈跟踪添加到问题中吗?@Dogbert,我已使用堆栈跟踪更新了问题。我遇到了几乎相同的问题,已经调试了一个小时没有任何发现你救了我一天。非常感谢。我遇到了几乎相同的问题,已经调试了一个小时,没有任何发现——你救了我一天。非常感谢。