Flash not fetched, call fetch_flash/2

Hello,
first : sorry for my poor english…

I am new on Elixir Phoenix, I have started a new example project but when I tried to go on localhost:4000/users i have this error : > flash not fetched, call fetch_flash/2

My router.ex :

defmodule Project.Router do
  use Project.Web, :router
  resources "/films", Project.FilmController
  resources "/users", Project.UserController

  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

  scope "/", Project do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
  end

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

My user_controller.ex :

defmodule Project.UserController do
  use Project.Web, :controller

  alias Project.User

  plug :scrub_params, "user" when action in [:create, :update]

  def index(conn, _params) do
    users = Repo.all(User)
    render(conn, "index.html", users: users)
  end

  def new(conn, _params) do
    changeset = User.changeset(%User{})
    render(conn, "new.html", changeset: changeset)
  end

  def create(conn, %{"user" => user_params}) do
    changeset = User.changeset(%User{}, user_params)

    case Repo.insert(changeset) do
      {:ok, _user} ->
        conn
        |> put_flash(:info, "User created successfully.")
        |> redirect(to: user_path(conn, :index))
      {:error, changeset} ->
        render(conn, "new.html", changeset: changeset)
    end
  end

  def show(conn, %{"id" => id}) do
    user = Repo.get!(User, id)
    render(conn, "show.html", user: user)
  end

  def edit(conn, %{"id" => id}) do
    user = Repo.get!(User, id)
    changeset = User.changeset(user)
    render(conn, "edit.html", user: user, changeset: changeset)
  end

  def update(conn, %{"id" => id, "user" => user_params}) do
    user = Repo.get!(User, id)
    changeset = User.changeset(user, user_params)

    case Repo.update(changeset) do
      {:ok, user} ->
        conn
        |> put_flash(:info, "User updated successfully.")
        |> redirect(to: user_path(conn, :show, user))
      {:error, changeset} ->
        render(conn, "edit.html", user: user, changeset: changeset)
    end
  end

  def delete(conn, %{"id" => id}) do
    user = Repo.get!(User, id)

    # Here we use delete! (with a bang) because we expect
    # it to always work (and if it does not, it will raise).
    Repo.delete!(user)

    conn
    |> put_flash(:info, "User deleted successfully.")
    |> redirect(to: user_path(conn, :index))
  end
end

And my App.html.eex :

<!DOCTYPE html>
<html lang="fr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="Jeremy">

    <title>FilmsGuide</title>
    <link rel="stylesheet" href="<%= static_path(@conn, "/css/app.css") %>">
  </head>

  <body>
    <div class="container">
      <header class="header">
        <nav role="navigation">
          <ul class="nav nav-pills pull-right">
            <li><a href="<%= static_path(@conn, "/")%>">Accueil</a></li>
            <li><a href="<%= static_path(@conn, "/users")%>">Utilisateurs</a></li>
            <li><a href="<%= static_path(@conn, "/films")%>">Films</a></li>
          </ul>
        </nav>
      </header>

      <p class="alert alert-info" role="alert"><%= get_flash(@conn, "info") %></p>
      <p class="alert alert-danger" role="alert"><%= get_flash(@conn, "error") %></p>

      <main role="main">
        <%= render @view_module, @view_template, assigns %>
      </main>

    </div> <!-- /container -->
    <script src="<%= static_path(@conn, "/js/app.js") %>"></script>
  </body>
</html>

Thank you !

3 Likes

Your routes are defined without a pipeline corresponding to them. Please move them to an appropriate scope. Also give the Phoenix guides on routing another read.

5 Likes

Thank you very much NobbZ ! There is the result of my router.ex :
defmodule Project.Router do
use Project.Web, :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

  scope "/", Project do
    pipe_through :browser # Use the default browser stack
    get "/", PageController, :index
    resources "/users", UserController
    resources "/films", FilmController
  end

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

Is this an “OK, now it works!” or some kind of follow up question?

Haha, is an “OK, now it works !” Thanks !

2 Likes