Isnor Creative
Isnor Creative Blog
Ruby, Ruby on Rails, Ember, Elm, Phoenix, Elixir, React, Vue

Oct 15, 2015

Phoenix Elixir Bootstrap alert flash helper

This is how I recently implemented a bootstrap alert helper for flash messages in a Phoenix app. It’s a pretty basic first solution that uses Elixir’s pattern matching to display the appropriate flash message type based on what’s currently set.

web/templates/layout/app.html.eex

 <%= show_flash(@conn) %>

web/views/layout_view.ex

defmodule MyApp.LayoutView do
    use MyApp.Web, :view

    def show_flash(conn) do
      get_flash(conn) |> flash_msg
    end

    def flash_msg(%{"info" => msg}) do
      ~E"<div class='alert alert-info'><%= msg %></div>"
    end

    def flash_msg(%{"error" => msg}) do
      ~E"<div class='alert alert-danger'><%= msg %></div>"
    end

    def flash_msg(_) do
      nil
    end

end

I am available for Elixir/Phoenix consulting work – get in touch to learn more.

Gordon B. Isnor

Gordon B. Isnor writes about Ruby on Rails, Ember.js, Elm, Elixir, Phoenix, React, Vue and the web.
If you enjoyed this article, you may be interested in the occasional newsletter.

I am now available for project work. I have availability to build greenfield sites and applications, to maintain and update/upgrade existing applications, team augmentation. I offer website/web application assessment packages that can help with SEO/security/performance/accessibility and best practices. Let’s talk

comments powered by Disqus