JulienCorb

JulienCorb

Protocol Enumerable not implemented for

I am trying to implement my new.html.eex file to create new posts on my website.

new.html.eex:

<h1>Create Post</h1>
<%= form_for @changeset, post_path(@conn, :create, @current_user.id), fn f -> %>
  <%= if @changeset.action do %>
    <div class="alert alert-danger">
      <p>There are some errors</p>
    </div>
  <% end %>
  <div class="form-group">
    <%= text_input f, :title, placeholder: "Title",
                              class: "form-control" %>
    <%= error_tag f, :title %>
  </div>
  <div class="form-group">
    <%= textarea f, :body, placeholder: "Body", class: "form-control" %>
    <%= error_tag f, :body %>
  </div>
  <%= submit "Create post", class: "btn btn-primary" %>
<% end %>

new and create methods in post_controller.ex:

  def new(conn, _params, current_user) do
    changeset =
    current_user
    |> Ecto.build_assoc(:posts)
    |> Post.changeset
    render conn, "new.html", changeset: changeset
  end

  def create(conn, %{"post" => post_params}, current_user) do
    changeset =
    current_user
    |> Ecto.build_assoc(:posts)
    |> Post.changeset(post_params)
    case Repo.insert(changeset) do
      {:ok, _} ->
        conn
        |> put_flash(:info, "Post was created successfully")
        |> redirect(to: post_path(conn, :index, current_user.id))
        {:error, changeset} ->
          render(conn, "new.html", changeset: changeset)
    end
  end

But I can’t access the template. I get the following error:

protocol Enumerable not implemented for 1. This protocol is implemented for: DBConnection.PrepareStream, DBConnection.Stream, Date.Range, Ecto.Adapters.SQL.Stream, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Postgrex.Stream, Range, Stream

Things seems to get wrong in the router, so here is my router.ex:

defmodule GazetteWeb.Router do
  use GazetteWeb, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    plug :protect_from_forgery
    plug :put_secure_browser_headers
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  pipeline :with_session do #if a user is signed in
    plug Guardian.Plug.VerifySession
    plug Guardian.Plug.LoadResource
    plug Gazette.CurrentUser
  end

  pipeline :admin_required do
    plug Gazette.CheckAdmin
  end

  pipeline :login_required do

  end

  scope "/posts", GazetteWeb do
    pipe_through [:browser, :with_session, :admin_required]
    resources "/", PostController, except: [:index, :show]
  end

  scope "/", GazetteWeb do
    pipe_through [:browser, :with_session]

    get "/", PageController, :index

    resources "/users", UserController, only: [:show, :new, :create]
    resources "/sessions", SessionController, only: [:new, :create, :delete]
    resources "/posts", PostController, only: [:index, :show]
  end


  # Other scopes may use custom stacks.
  # scope "/api", GazetteWeb do
  #   pipe_through :api
  # end
end

any Idea ?

Marked As Solved

JulienCorb

JulienCorb

the stacktrace:

 elixir /private/tmp/elixir-20180111-94038-1oxcohh/elixir-1.5.3/lib/elixir/lib/enum.ex:1 Enumerable.impl_for!/1
 elixir /private/tmp/elixir-20180111-94038-1oxcohh/elixir-1.5.3/lib/elixir/lib/enum.ex:116 Enumerable.reduce/3
 elixir lib/enum.ex:1850 Enum.reduce/3
 lib/gazette_web/router.ex:1 GazetteWeb.Router.Helpers.segments/3
 lib/gazette_web/router.ex:1 GazetteWeb.Router.Helpers.post_path/3
 lib/gazette_web/templates/post/new.html.eex:2 GazetteWeb.PostView."new.html"/1
 lib/gazette_web/templates/layout/app.html.eex:26 GazetteWeb.LayoutView."app.html"/1
 phoenix lib/phoenix/view.ex:332 Phoenix.View.render_to_iodata/3
 phoenix lib/phoenix/controller.ex:740 Phoenix.Controller.do_render/4
 lib/gazette_web/controllers/post_controller.ex:1 GazetteWeb.PostController.action/2
 lib/gazette_web/controllers/post_controller.ex:1 GazetteWeb.PostController.phoenix_controller_pipeline/2
 lib/gazette_web/endpoint.ex:1 GazetteWeb.Endpoint.instrument/4
 phoenix lib/phoenix/router.ex:278 Phoenix.Router.__call__/1
 lib/gazette_web/endpoint.ex:1 GazetteWeb.Endpoint.plug_builder_call/2
 lib/plug/debugger.ex:99 GazetteWeb.Endpoint."call (overridable 3)"/2
 lib/gazette_web/endpoint.ex:1 GazetteWeb.Endpoint.call/2
 plug lib/plug/adapters/cowboy/handler.ex:15 Plug.Adapters.Cowboy.Handler.upgrade/4
  • UPDATE: I got it working by removing @current_user from the controller and the template.

However, I am wondering if it works right: here is my DB:

iex(2)> Gazette.Repo.all(Gazette.Post)
[debug] QUERY OK source="posts" db=3.2ms decode=5.6ms
SELECT p0."id", p0."body", p0."title", p0."user_id", p0."inserted_at", p0."updated_at" FROM "posts" AS p0 []
[%Gazette.Post{__meta__: #Ecto.Schema.Metadata<:loaded, "posts">,
  body: "Alors voici mon tout premier post. J'espère qu'il en appellera bien d'autres, de bonne qualité et plaisant pour les lecteurs. J'ai hâte que tout cela démarre ! ",
  id: 1, inserted_at: ~N[2018-02-12 17:05:54.966289],
  title: "Mon tout Premier Post !! ",
  updated_at: ~N[2018-02-12 17:05:54.966325],
  user: #Ecto.Association.NotLoaded<association :user is not loaded>,
  user_id: 1},

What is this user: #Ecto.Association.NotLoaded<association :user is not loaded>, ??

Also Liked

idi527

idi527

You would first need to preload it.

For example,

Repo.preload(post, :user)
idi527

idi527

What is this user: #Ecto.Association.NotLoaded, ??

It’s something like a placeholder. It allows for lazy-loading resources.

idi527

idi527

The problem seems to be that post_path(@conn, :create, @current_user.id) accepts 3 arguments, where the last is a list of options. So you should probably change it to post_path(@conn, :create).

With options it would be

post_path(@conn, :create, to: "a", by: "b")
# which is the same as
post_path(@conn, :create, [to: "a", by: "b"])
# which is the same as
post_path(@conn, :create, [{:to, "a"}, {:by, "b"}])
# and results in
"/posts?to=a&by=b"

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New

We're in Beta

About us Mission Statement