Hi there!
I’ve used phoenix.gen.html
to build a UserAccount
resource. A UserAccount
can either belong to a Client
or an Employee
, so I need to add one of these associations to the account.
I’m not sure not what’s the best way to do it. I think I need to add the information to the URL, so that I can add a button “Create UserAccount” to an employee’s or a client’s page.
It might be easier to understand in code:
defmodule App.UserAccountController do
use App.Web, :controller
alias App.{Client, Employee, UserAccount}
def new(conn, _params) do
# Would it make sense to build an association here already?
# I've tried that an error:
# protocol Phoenix.HTML.FormData not implemented for %App.UserAccount...
changeset = UserAccount.changeset(%UserAccount{})
render(conn, "new.html", changeset: changeset)
end
def create(conn, %{"user_account" => user_account_params}) do
# Owner can be a Client or an Employee
# How can I get its type and id?
owner = Client
owner = Employee
user_account = Repo.one!(owner, owner_id) |> build_assoc(:user_account)
changeset = UserAccount.changeset(user_account, user_account_params)
case Repo.insert(changeset) do
# ...
end
end
end
I hope it’s clear what I want to achieve, if not I’ll try to explain it differently
Thanks!
Edit: I just found the following and think that I could use query params as a simple solution, but then I’d need to build the association in the new
function already. How could I do that? (http://www.phoenixframework.org/docs/routing#section-more-on-path-helpers)