elixirmoney / money

Elixir library for working with Money safer, easier, and fun... Is an interpretation of the Fowler's Money pattern in fun.prog.
https://hex.pm/packages/money/
MIT License
826 stars 139 forks source link

Protocol Undefined Error when rendering the amount on a Form #197

Closed Manukam closed 1 year ago

Manukam commented 1 year ago

** (Protocol.UndefinedError) protocol Phoenix.HTML.Safe not implemented for %{"amount" => 100000, "currency" => :EUR} of type Map.

I have defined Money.Ecto.Amount.Type in an embedded schema, and I am trying to render the amount as shown below, <%= text_input(fd, :cost, class: input_class) %>

This gives the above error

Money - 1.12

Nitrino commented 1 year ago

Hi @Manukam I think this will not work in the current implementation, you need to pass only value from money struct. I will experiment how to implement it

Manukam commented 1 year ago

Hi @Nitrino, thanks for the quick reply. I will provide more information. This is how I have defined the schema,

@type t() :: %__MODULE__{
          name: String.t(),
          cost: Money.Ecto.Amount.Type
        }

  embedded_schema do
    field :name, :string
    field :cost, Money.Ecto.Amount.Type
  end

In the form, this is a add more use case. Using a button the user can add more of these to the form,

impl true
  def handle_event("add_more", _params, socket) do
    socket.assigns.something
    |> Map.update!(:field, &(&1 ++ [%Struct{}]))
    |> update_something(socket)
  end

During this handle_event this error pops up