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.'
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 %>
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With