Oxyrus

Oxyrus

Check for error and info alert in Phoenix

In Rails we’re able to use

<% if info %>, however in Phoenix we don’t have that option, in the phoenix channel on Slack a guy pointed me to http://stackoverflow.com/questions/34068589/elixir-phoenix-flash-messages-do-not-show-up however it doesn’t work either, creating a file in /web/templates/layout/alert.html.eex with the content provided in the example throws an error:

Compiling 1 file (.ex)

== Compilation error on file web/views/layout_view.ex ==
** (CompileError) web/templates/layout/alert.html.eex:1: undefined function info/0
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:117: anonymous fn/4 in Kernel.ParallelCompiler.spawn_compilers/1

What is the way to do it correctly?

Marked As Solved

alexgaribay

alexgaribay

Phoenix Core Team
<%= if get_flash(@conn, :info) do %>
  <span><%= get_flash(@conn, :info) %></span>
<% end %>

You can replace :info with whatever atom you specify in put_flash.

Also Liked

cpgo

cpgo

I have something like this. There is probably a much better way of doing it.

defmodule Learnp.LayoutView do
  use Learnp.Web, :view
  import Learnp.Router.Helpers

  def show_notification(conn) do
    conn
    |> get_flash
    |> flash_message
  end

  def flash_message(%{"info" => message}) do
    render "components/_notification.html", class: "primary", message: message
  end

  def flash_message(%{"error" => message}) do
    render "components/_notification.html", class: "danger", message: message
  end

  def flash_message(_), do: nil

end

And on my templates I can call it with <%= show_notification(@conn)%> and it will render the partial web/templates/layout/components/_notification.html.eex.

cpgo

cpgo

It’s just some HTML with Bootstrap markup

<div class="callout <%= @class %>" data-closable>
  <button class="close-button" aria-label="Fechar alerta" type="button" data-close>
    <span aria-hidden="true">&times;</span>
  </button>
     <%= @message %>
</div>

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
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement