指定@changeset在eex模板中不可用 [英] assign @changeset not available in eex template

查看:623
本文介绍了指定@changeset在eex模板中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习包括Ecto.Model在内的Phoenix表单系统,但是遇到了我无法通过的问题.我已经创建了一个表单:

I am trying to learn the Phoenix's Form system with including the Ecto.Model's but i have encountered a problem that i can't pass. I have created a form:

<div class="jumbotron">
  <%= form_for @changeset, user_path(@conn, :create), fn f -> %>
    <label>
      Login: <%= text_input f, :login %>
    </label>
    <label>
      Password: <%= password_input f, :password %>
    </label>
    <label>
      Name: <%= text_input f, :name %>
    </label>
    <label>
      Surname: <%= text_input f, :name %>
    </label>
    <label>
      Email: <%= email_input f, :name %>
    </label>
    <label>
      Class: <%= text_input f, :name %>
    </label>
    <label>
      Phone: <%= text_input f, :name %>
    </label>

    <%= submit "Submit" %>
  <% end %>
</div>

这是由控制器提供的:

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

  def create(conn, _params) do
    IO.inspect(_params)
    render conn, "index.html"
  end

和模型:

defmodule Kpsz.Model.User do
  use Kpsz.Web, :model

  schema "users" do
    field :login, :string
    field :password, :string
    field :email, :string

    field :role, :integer

    field :name, :string
    field :surname, :string
    field :class, :string
    field :phone_number, :string

    has_many :presences, Kpsz.Model.Presence
  end

  @required_fields ~w(login,password,email,name,surname,class,phone_number)
  @optional_fields ~w(role)

  def changeset(user, params \\ :empty) do
    case params do
      :empty -> cast(user,params, ~w(),~w())
      _ -> user
        |> cast(params, @required_fields, @optional_fields)
        |> validate_format(:email, ~r/@/)
        |> unique_constraint(:email)
    end
  end

end

我有与参数匹配的模式,因为在创建用于将其传递给表单的空变更集时遇到了很多错误.有什么更好的办法吗?

I have pattern matched the params, because i was getting bunch of errors while creating empty changeset for passing it to the form. Is there any better way around it?

提交表单后出现错误:

And error I am getting after submiting the form:

谁能指出我做错了什么,并提供一些解决方法?

Can anyone point out what I am doing wrong and give some info how to fix it?

推荐答案

您正在通过两个动作呈现相同的视图和模板.

You are rendering the same view and template from both actions.

此行:

<%= form_for @changeset, user_path(@conn, :create), fn f -> %>

参考文献@changeset,预期会通过分配传递给您的Eex模板.在phoenix控制器中,您可以通过以下方式进行此操作:

References @changeset which is expected to be passed to your Eex template via assigns. In a phoenix controller you do this by calling:

render(conn, template, assigns)

在您的情况下,您要在index上传递变更集,但不能在create上传递变更集.通常,您的表单将在new函数中呈现,而参数将在create函数的changeet函数中使用.

In your case you are passing the changeset on index but not on create. Normally your form will be rendered in the new function and the params will be used in the changeset function in the create function.

考虑生成此代码以查看其工作方式:

Consider generating this code to see how it works:

mix phoenix.gen.html User name surname email age:integer

这篇关于指定@changeset在eex模板中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆