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 ?