Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a newline to the flash error?

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?

like image 690
user612308 Avatar asked Jan 06 '12 03:01

user612308


1 Answers

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
like image 166
Blair Anderson Avatar answered Oct 09 '22 09:10

Blair Anderson