Looping Through Flash in Rails 5


Step 1

In app/views/layouts/application.html.erb layout file add:

<% flash.each do |key, msg| %>
    <%= content_tag :div, msg, id: key %>
<% end %>

The layout file now looks like this:

<!DOCTYPE html>
    <title>Todo List</title>
    <%= csrf_meta_tags %>
    <%= action_cable_meta_tag %>

    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track' => 'reload' %>

  <% flash.each do |key, msg| %>
    <%= content_tag :div, msg, id: key %>
  <% end %>

     <div id='container'>
            <%= yield %>


Step 2

Remove from notice section in the app/views/products/show.html.erb:

<p id="notice"><%= notice %></p>

Step 3

Edit the product. You will see the flash notice rendered on the product show page.

Related Articles

Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.