1
votes

I am following this tutorial building-a-simple-stream-media-app

When i go to change the list to Struct Not quite sure what i am doing, really new to this. it's not working, keep getting

protocol Enumerable not implemented

My controller

defmodule Blog.PageController do
      use Blog.Web, :controller

      def index(conn, _params) do
        media_dir = "./priv/static/media/"
        filetype = [".mp4", ".png"]
        {:ok, files} = File.ls(media_dir)
        filtered_files = Enum.filter(files, fn(file) -> String.ends_with?(file, [".mp4", ".png"]) end)
        struct_files = Enum.map(filtered_files, fn(file) -> %Blog.Media{filename: file} end )
        render conn, "index.html", files: struct_files
      end

      def show(conn, %{"filename" => filename}) do
        render conn, "show.html", filename: filename
      end
    end

Model

defmodule Blog.Media do
  @derive {Phoenix.Param, key: :filename}
  defstruct [:filename]
end
1
Please provide full error messagemichalmuskala
The line number in particular would be helpful.Cameron Price
protocol Enumerable not implemented for %Blog.Media{filename: "findingnemo.mp4"} This is the error message.codedownforwhat

1 Answers

3
votes

You cant derive Enumerable in the tutorial he mentions Elixir 1.1 but the tutorial github uses 1.0 - Also see this issue. https://github.com/elixir-lang/elixir/issues/3821. Just return a map instead of a struct.

def index(conn, _params) do
    render conn, "index.html", files: media_files
end

defp media_files do
    media_dir = "</your/file_path/>"
    {:ok, files} = File.ls(media_dir)
    filetype = [".mp4", ".mp3"]

    Enum.filter(files, fn(file) -> 
      String.ends_with?(file, filetype)
    end)
    |> Enum.map(fn(file) -> 
      %{filename: file} # Map
    end)
end