2017-06-15 38 views
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 
+1

你能發佈'Contact.create_email'的定義?它接受變更集還是隻是attrs?我想你想要做'Contact.create_email(email_params)'。 – Dogbert

+0

@Dogbert剛剛添加到OP –

回答

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 
+0

這是修復它,我可以看到數據庫中的新紀錄。謝謝@dogbert –