简体   繁体   English

视图中未定义的功能conn / 0

[英]Undefined function conn/0 in View

I am working through the Programming Phoenix 1.4 book, and while creating my view for Users, I ran into an issue. 我正在阅读《编程Phoenix 1.4》一书,在为用户创建视图时遇到了一个问题。 I continually get a compile error saying 我不断收到编译错误提示

== Compilation error in file lib/rumbl_web/views/user_view.ex ==
** (CompileError) lib/rumbl_web/views/user_view.ex:3: undefined function conn/0
    (elixir) src/elixir_locals.erl:107: :elixir_locals."-ensure_no_undefined_local/3-lc$^0/1-0-"/2
    (elixir) src/elixir_locals.erl:107: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:208: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6`

when attempting to compile and run the project. 尝试编译和运行项目时。 Here is the view in question: 这是有问题的视图:

defmodule RumblWeb.UserView do
  use RumblWeb, :view
  alias Rumbl.Accounts

  def first_name(%Accounts.User{name: name}) do
    name
    |> String.split(" ")
    |> Enum.at(0)
  end

  def username(%Accounts.User{username: username}) do
    username
  end

end

If I comment out the line use RumblWeb, :view , the project compiles (although it cannot render the view for obvious reasons). 如果我use RumblWeb, :view注释掉该行,则项目会编译(尽管出于明显的原因它无法呈现视图)。 I am rather new to phoenix and elixir, so this has been pretty perplexing. 我对凤凰和长生不老药比较陌生,所以这非常令人困惑。

If I add conn to the third line of views/user_view.ex : 如果我将conn添加到views/user_view.ex的第三行:

defmodule RumWeb.UserView do
  use RumWeb, :view
  conn  #====== *** HERE ***
  alias Rum.Accounts.User

  def first_name(%User{name: name} ) do
    name
    |> IO.inspect 
    |> String.split(" ")
    |> Enum.at(0)
  end


end

I get almost the same error as you: 我收到与您几乎相同的错误:

$ mix phx.server
Compiling 9 files (.ex)
warning: variable "conn" does not exist and is being expanded to "conn()", please use parentheses to remove the ambiguity or change the variable name
  lib/rum_web/views/user_view.ex:3


== Compilation error in file lib/rum_web/views/user_view.ex ==
** (CompileError) lib/rum_web/views/user_view.ex:3: undefined function conn/0
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:198: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/6

I don't have the following in the middle of my error: 我的错误中间没有以下内容:

ensure_no_undefined_local/3-

but maybe that's due to differences in elixir versions. 但这可能是由于e剂版本不同。

If I put conn anywhere in rumbl_web.ex , then the error points to rumbl_web.ex --not user_view.ex . 如果将conn放在rumbl_web.ex任何地方,则错误指向rumbl_web.ex user_view.ex Are you posting all the output after you do $ mix phx.server ? 执行$ mix phx.server之后,是否要发布所有输出?

Are you sure you don't have two windows open with two different versions of the file user_view.ex ? 您确定没有打开两个带有两个不同版本的文件user_view.ex吗? I would close all your editor's windows, then reopen your project and look at user_view.ex again. 我将关闭所有编辑器的窗口,然后重新打开项目并再次查看user_view.ex

I had a weird error one time, and the solution was: 我有一次奇怪的错误,解决方法是:

../rumbl$ rm -rf _build

then do: 然后做:

../rumbl$ mix phx.server 

and mix will rebuild your project, recreating the _build directory in the process. and mix将重建您的项目,并在此过程中重新创建_build目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM