Flash Message Best Practices

I am wondering what is the best practice for displaying flash messages. The two main ways I've seen use something like this code generated by the scaffold.

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

or placing code in the application header.

 <% if !flash.empty? %> <div id="flash"> <% flash.keys.each do |k| %> <div class="<%= k %>"> <%= flash[k] %> </div> <% end %> </div> <% end %> 

It seems to me that the first method improves flexibility, while the latter improves code readability and eliminates redundancy. Is there a method that most rails developers prefer? How is the question of how scaffolding implemented? Is it just an assistant that accesses the flash hash? Why worry about using an assistant when you can directly use hash flash? Thanks

+23
ruby-on-rails ruby-on-rails-3
Feb 22 '12 at 7:35
source share
3 answers

I do this:

 <% flash.each do |key, value| %> <%= content_tag :div, value, class: "flash #{key}" %> <% end %> 
+53
Feb 22 2018-12-22T00:
source share

Calling to partially save your application.html.erb even cleaner.

 <%= render 'shared/flash_messages' if !flash.empty? %> 

.. and in partial doing something like what @zolter said:

 <div id="flash_messages"> <% flash.each do |key, value| %> <%= content_tag(:div, value, :class => "flash #{key}") %> <% end %> </div> 
+30
Feb 23 2018-12-23T00:
source share

Why not put the second method on an auxiliary function so that it does not affect the readability of the code on the layouts?

+2
Feb 22 '12 at 7:45
source share



All Articles