Looping Through Flash in Rails 5

Steps

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>
<html>
  <head>
    <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' %>
  </head>

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

  <body>
     <div id='container'>
         <h1></h1>
            <%= yield %>
     </div>
  </body>

</html>

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


Create your own user feedback survey