2
我有一個聯繫表單,使用下面的變更集。試圖在Phoenix 1.3中提交表單時,預期參數爲地圖錯誤
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: "[email protected]", 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
你能發佈'Contact.create_email'的定義?它接受變更集還是隻是attrs?我想你想要做'Contact.create_email(email_params)'。 – Dogbert
@Dogbert剛剛添加到OP –