Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 5 redirect notice style

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.'
like image 399
Trenton Tyler Avatar asked Dec 18 '22 05:12

Trenton Tyler


2 Answers

Add a class to a tag, maybe a div, and then wrap your notice there, like:

<div class="notice">
  <%= notice %>
</div>

But what's usually done, is to assign a class to the html tag dinamycally, this way if the flash message is notice or other, then you have the styles defined for each of them, like:

<% flash.each do |key, message| %> 
  <p class="<%= key %>">
    <%= message %>
  </p> 
<% end %>
like image 73
Sebastian Palma Avatar answered Dec 27 '22 19:12

Sebastian Palma


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

like image 34
Paulo Belo Avatar answered Dec 27 '22 17:12

Paulo Belo