(ArgumentError) you attempted to apply :path on "beacon/company-logo/toiudkazny6rashasl3u"

Hey, guys, I need some help to understand what is happening here, so I want to upload a file to Cloudinary, and I have everything set up but the error I’m facing is

** (ArgumentError) you attempted to apply :path on "beacon/company-logo/toiudkazny6rashasl3u". If you are using apply/3, make sure the module is an atom. If you are using the dot syntax, such as map.field or module.function, make sure the left side of the dot is an atom or a map
    :erlang.apply("beacon/company-logo/toiudkazny6rashasl3u", :path, [])
    (beacon) lib/beacon/upload.ex:8: Beacon.Upload.upload_attachement/4
    (beacon) lib/beacon/account/company.ex:19: Beacon.Account.Company.changeset/2
    (beacon) lib/beacon/account/user_actions.ex:117: Beacon.Account.UserActions.upsert_brand/1

My controller looks like this

def create(conn, %{"brand_logo" => brand_logo, "brand_color" => brand_color, "welcoming_message" => welcoming_message}) do
    case UserActions.upsert_company(%{ brand_logo: brand_logo, brand_color: brand_color, welcoming_message: welcoming_message }) do
      {:ok, brand_logo, brand_color, welcoming_message} ->
      json conn, %{}
    end
  end

and the action in which I’m doing the insert_or_update into the database is

  def upsert_company(attrs) do
    case get_company_brand() do
      nil -> %Company{}
      company -> company
    end
    |> Company.changeset(attrs)
    |> Repo.insert_or_update()
    |> IO.inspect
  end

schema is

schema "companies" do
    field :brand_color, :string
    field :brand_logo, :string
    field :welcoming_message, :string

    field :brand_logo_url, :string, virtual: true

    timestamps()
  end

  def changeset(company, attrs) do
    company
    |> cast(attrs, [:brand_color, :brand_logo, :welcoming_message])
    |> Upload.upload_attachement(attrs, :brand_logo, %{
      tags: ["company-logo", "brand_logo"],
      folder: Application.fetch_env!(:cloudex, :prefix) <> "/company-logo/",
    })
    |> validate_required([:brand_color])
  end

Now I’m not sure why is he telling me that I want to apply path, I just want to save that id which is returning from the Cloudinary which is string, and later fetch it to display it in my template, so can someone please help me understand what I’m I doing wrong here, thanks.

The stack trace points to lib/beacon/upload.ex:8: Beacon.Upload.upload_attachement/4

What’s happening there?

hey in the changeset I’m defining upload to cloudex

def changeset(company, attrs) do
    company
    |> cast(attrs, [:brand_color, :welcoming_message])
    |> Upload.upload_attachement(attrs, :brand_logo, %{
      tags: ["company-logo", "brand_logo"],
      folder: Application.fetch_env!(:cloudex, :prefix) <> "/company-logo/",
    })
    |> validate_required([:brand_color])
  end

and upload_attachement is

  def upload_attachement(changeset, attrs, key, cloudex_params \\ %{}) do
    file = attrs[key] || attrs[to_string(key)]

    if !changeset.valid? || file == nil || file.path == nil do
      changeset
    end
      {:ok, %{public_id: public_id}} = Cloudex.upload(file.path, cloudex_params)

      old_file = get_field(changeset, key)
      if old_file do
        Cloudex.delete_prefix(old_file)
      end

      changeset
      |> put_change(key, public_id)
  end

I totally overlooked at this code here.

You are calling file.path but file is a string.

4 Likes

Double-check the form that’s being used to upload the file; I remember similar errors from Rails code when the form wasn’t correctly set to multipart.

@al2o3cr sure, will do that, I think it was a typo in the code, but nevertheless thanks