Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 菲尼克斯/长生不老药/埃克托.卡斯特罗:价值“;新的在“where”中,不能在MyApp.User中的查询:from u中转换为type:id,_Elixir_Phoenix Framework_Ecto - Fatal编程技术网

Elixir 菲尼克斯/长生不老药/埃克托.卡斯特罗:价值“;新的在“where”中,不能在MyApp.User中的查询:from u中转换为type:id,

Elixir 菲尼克斯/长生不老药/埃克托.卡斯特罗:价值“;新的在“where”中,不能在MyApp.User中的查询:from u中转换为type:id,,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我正在使用编程Phoenix,在我的应用程序中添加一个新的用户功能: 我得到以下错误: deps/ecto/lib/ecto/repo/queryable.ex:188: value `"new"` in `where` cannot be cast to type :id in query: from u in Microflow.User, where: u.id == ^"new" Error when casting value to `Microflow.User.id` dep

我正在使用编程Phoenix,在我的应用程序中添加一个新的用户功能:

我得到以下错误:

deps/ecto/lib/ecto/repo/queryable.ex:188: value `"new"` in `where` cannot be cast to type :id in query:

from u in Microflow.User,
where: u.id == ^"new"

Error when casting value to `Microflow.User.id`
deps/exto/lib/exto/repo/queryable.ex:188之前的代码块如下:

defp query_for_get(_repo, queryable, id) do
  query = Queryable.to_query(queryable)
  model = assert_model!(query)
  primary_key = primary_key_field!(model)
  Ecto.Query.from(x in query, where: field(x, ^primary_key) == ^id) 
第188行:

此处的用户控制器更新:

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.name} created!")
      |> redirect(to: user_path(conn, :index))
  {:error, changeset} ->
    render(conn, "new.html", changeset: changeset)
  end
end
这里的EEX示例:

<h1>New User</h1>

 <%= form_for @changeset, user_path(@conn, :create), fn f -> %>
    <div class="form-group">

 <%= text_input f, :name, placeholder: "Name", class: "form-control" %>
    </div>

<%= form_for @changeset, user_path(@conn, :create), fn f -> %>
  <div class="form-group">

<%= text_input f, :name, placeholder: "Name", class: "form-control" %>
  </div>

<div class="form-group">
<%= text_input f, :username, 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 %>
新用户
%>
%>
你知道我能做什么吗?如果你能帮忙,谢谢你

相关讨论如下:


通过删除此行修复:

get "/users/:id", Microflow.UserController, :show
并加上:

resources "/users", Microflow.UserController 

我不知道为什么一个小的改变会引起这样的错误,但我已经重新检查了5次。我见过许多路由错误,但没有一个会产生如此模糊的错误消息

它引发该错误是因为
/users/new
路由是特殊的,当使用
资源
资源
路由到
show
操作时,它会路由到
操作,当只有
:show
路由存在或它是第一个路由时,它会路由到
show
操作
resources
/users/:id
路由之前内部声明了一个
/users/new
路由,因此
new
不会创建为要显示的id。
resources "/users", Microflow.UserController