Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is cast_attachments changing my params? #5

Closed
MartinElvar opened this issue Sep 9, 2015 · 7 comments
Closed

Is cast_attachments changing my params? #5

MartinElvar opened this issue Sep 9, 2015 · 7 comments

Comments

@MartinElvar
Copy link

When using cast_attachments/4, Phoenix seem to choke, with the following error, while rendering my form.

Request: POST /admin/companies/6
** (exit) an exception was raised:
    ** (FunctionClauseError) no function clause matching in Phoenix.HTML.Safe.Tuple.to_iodata/1
        (phoenix_html) lib/phoenix_html/safe.ex:72: Phoenix.HTML.Safe.Tuple.to_iodata({"sadfasdfasdf", %Mobil.Company{__meta__: #Ecto.Schema.Metadata<:loaded>, id: 6, inserted_at: #Ecto.DateTime<2015-09-09T15:12:24Z>, logo: %{file_name: "høstning.png", updated_at: #Ecto.DateTime<2015-09-09T15:12:24Z>}, name: nil, updated_at: #Ecto.DateTime<2015-09-09T15:12:24Z>}})
        (phoenix_html) lib/phoenix_html/tag.ex:66: anonymous fn/2 in Phoenix.HTML.Tag.tag_attrs/1
        (elixir) lib/enum.ex:1261: Enum."-reduce/3-lists^foldl/2-0-"/3
        (phoenix_html) lib/phoenix_html/tag.ex:35: Phoenix.HTML.Tag.tag/2
        (mobil) web/templates/admin/company/form.html.eex:15: anonymous fn/2 in Mobil.Admin.CompanyView.form.html/1
        (phoenix_html) lib/phoenix_html/form.ex:227: Phoenix.HTML.Form.form_for/4
        (mobil) web/templates/admin/company/form.html.eex:1: Mobil.Admin.CompanyView."form.html"/1
        (mobil) web/templates/admin/company/edit.html.eex:7: Mobil.Admin.CompanyView."edit.html"/1

As Phoenix tries to render

<%= text_input f, :name, class: "form-control" %>

Which suggest, that my name value was changed?

If i change it back to cast/4, then the problem is gone, but then i'm missing my image.

@stavro
Copy link
Owner

stavro commented Sep 9, 2015

Can you share your cast and cast_attachments code? Currently it is designed where you need to use cast_attachments for all file attributes only. All other attributes should still be sent through cast.

@MartinElvar
Copy link
Author

This is my model.

defmodule Mobil.Company do
  use Mobil.Web, :model
  use Arc.Ecto.Model

  schema "companies" do
    field :name, :string
    field :logo, Mobil.Logo.Type

    timestamps
  end

  @required_fields ~w(name)
  @optional_fields ~w(logo)

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast_attachments(params, @required_fields, @optional_fields)
  end
end

So i need to split it up?

@stavro
Copy link
Owner

stavro commented Sep 9, 2015

Yes, please split it up as such: https://github.com/stavro/arc_ecto/blob/master/test/model_test.exs#L21 and try again

@MartinElvar
Copy link
Author

Worked wonders! Thank you @stavro

@stavro
Copy link
Owner

stavro commented Sep 9, 2015

I will do some digging to see if there is a less confusing way to handle this... You are not the first person to make this same hiccup

@MartinElvar
Copy link
Author

@stavro I don't know if i think that it is confusing, rather i would say the readme could be clearer.

More specific. https://github.com/stavro/arc_ecto#add-your-attachment-to-your-ecto-model

Which could explain it more like..

  schema "companies" do
    field :name, :string
    field :logo, Mobil.Logo.Type

    timestamps
  end

  @required_fields ~w(name)
  @optional_fields ~w()

  @doc """
  Creates a changeset based on the `model` and `params`.

  If no params are provided, an invalid changeset is returned
  with no validation performed.
  """
  def changeset(model, params \\ :empty) do
    model
    |> cast(params, @required_fields, @optional_fields)
    |> cast_attachments(params, ~w(), ~w(logo))
  end

@stavro
Copy link
Owner

stavro commented Sep 9, 2015

Readme updated! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants