Rails 5 redirect notice style

Viewed 3569

When a user creates a ticket my site redirects to the ticket and displays a notice that informs the user it has been created. At the moment it is a standard notice with no styling.

This is the block that redirects - I need to add a class to the notice. How can this be achieved?

redirect_to @ticket, notice: 'Ticket was successfully created.'
2 Answers

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

class ApplicationController < ActionController::Base
  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:

<% flash.each do |message_type, message| %>
  <div class="alert alert-<%= message_type %>">
    <%= message %>
  </div>
<% end %>

source: http://api.rubyonrails.org/v5.1/classes/ActionController/Flash/ClassMethods.html#method-i-add_flash_types

Related