I tried to format the flash error and ran into some issues:
errors = ["error1 msg", "error2 msg", "error3 msg"]
flash[:error] = errors.join("\n")
I expected to see them displayed in three separate lines, however, I got "error1 msg error2 msg error3 msg"
instead.
What did happen to the newline I used to join the string?
HTML does not use newlines for line-breaks, it uses <br/>
tags. Also, you must use html_safe to make sure the template doesn't escape the <br/>
tags
errors = ["error1 msg", "error2 msg", "error3 msg"]
flash[:error] = errors.join("<br/>").html_safe
Also, if you have an object with errors, you can use full_messages
to get nicely formatted errors. Something like this:
flash[:error] = @user.errors.full_messages.join("<br/>").html_safe
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