Skip to content

Instantly share code, notes, and snippets.

@pjbelo
Last active June 9, 2022 12:34
Show Gist options
  • Save pjbelo/eafff9c01c49aba66f45beb2afbb903b to your computer and use it in GitHub Desktop.
Save pjbelo/eafff9c01c49aba66f45beb2afbb903b to your computer and use it in GitHub Desktop.
[Rails] Flash Types

In rails 5 you can use add_flash_types method. Just add it to ApplicationController and include the types you want:

# application_controller.rb
...
  add_flash_types :success, :warning, :danger, :info

on your controller use the appropriate type instead of 'notice':

redirect_to @ticket, success: 'Ticket was successfully created.'

and then you can automate your view:

# application.html.erb
...
<% flash.each do |message_type, message| %>
  <div class="alert alert-<%= message_type %>">
    <%= message %>
    <button class="close" data-dismiss="alert">&times;</button>
  </div>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment