尝试在Phoenix 1.3中提交表单时,预期的params是一个映射错误

时间:2017-06-15 08:13:04

标签: phoenix-framework ecto

我有一个联系表格,使用以下变更集。

def changeset(%Email{} = email, attrs) do

    email
    |> cast(attrs, [:name, :email, :message, :event])
    |> validate_required([:name, :email, :message])
  end

但是当我尝试提交表单时出现以下错误:

expected params to be a map, got: `#Ecto.Changeset<action: nil, changes: %
{email: "cooper.sf@gmail.com", event: "wedding", message: "hello", name: "Simon"}, 
errors: [], data: #Iotc.Contact.Email<>, valid?: true>`

我的创建功能是:

  def create(conn, %{"email" => email_params}) do
    changeset = Contact.Email.changeset(%Iotc.Contact.Email{}, email_params)
    case Contact.create_email(changeset) do
      {:ok, email} ->
        conn
        |> put_flash(:info, "Email sent")
        |> redirect(to: email_path(conn, :index))
      {:error, changeset} ->
        conn
        |> put_flash(:error, "Something went wrong")
        |> render("index.html", changeset: changeset)
    end
  end

我假设它没有拿起attrs

来自contact.ex:

  def create_email(attrs \\ %{}) do
    %Email{}
    |> Email.changeset(attrs)
    |> Repo.insert()
  end

1 个答案:

答案 0 :(得分:1)

Contact.create_email/1需要一个attrs地图(不是变更集),并调用Contact.Email.changeset/2本身,因此您需要将email_params传递给它:

def create(conn, %{"email" => email_params}) do
  case Contact.create_email(email_params) do
    ...
  end
end