Skip to content

Instantly share code, notes, and snippets.

@elvisgiv
Last active April 27, 2016 09:48
Show Gist options
  • Save elvisgiv/5d4887ea0a82b973ee50e097a64f86de to your computer and use it in GitHub Desktop.
Save elvisgiv/5d4887ea0a82b973ee50e097a64f86de to your computer and use it in GitHub Desktop.

Вывод textarea в отформатированном виде во вью

../app/helpers/application_helper.rb

module ApplicationHelper
  def nl2br(s)
    s.gsub(/\n/, '<br>')
  end
end

метод def nl2br(s) возвращает строку с замененным /\n/ на <br>

чтобы <br> не отображался во вью, а вместо него был конец строки, применяем к строке метод html_safe

../app/views/user/show.html.haml

some code
  = (nl2br @user.admin_notes || "").html_safe
some code

= (nl2br @user.admin_notes || "").html_safe аналогично = (nl2br @user.admin_notes).html_safe if @user.admin_notes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment