I have a flash[:success]
message which goes like this:
flash[:success] = "This text is now bold."
.
However, when I go to make the text bold, it just wraps HTML characters around the message, rather than actually turning bold.
<b>This text is now bold</b>
How can I go about including HTML into flash messages?
In order to implement flash in your own apps, there's a specific set of steps that must be taken. You first call and set flash in your action controller. You must tell flash precisely what you want it to persist forward. Redirect your action controller to the full-page reload of your choice.
A flash message is a way to communicate information with the users of your Rails application so they can know what happens as a result of their actions. Example messages: “Password changed correctly” (confirmation) “User not found” (error)
Use <%= flash[:success].html_safe %>
in your view.
Whenever your flash[:success]
is blank, it will show error due to html_safe
. So it is better to use a condition.
So try with the following to prevent that error:
<%= flash[:success].html_safe unless flash[:success].blank? %>
You could also use .try
to prevent that error:
<%= flash[:success].try(:html_safe) %>
And if you know there is content for sure, you can also try:
<%= raw flash[:success] %>
On top of that, since you are using ERB, you can use h()
method for HTML-escaped strings:
<%= h flash[:success] %>
Check out this tutorial on ERB for other options such as for displaying JSON or URL-encoded strings.
save message as
flash[:success] = "<b>This text is now bold.</b>"
put in the html file as
<div class="notice">
<%=h flash[:notice]%>
</div>
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