【问题标题】:Textarea with whitespace preserved保留空格的文本区域
【发布时间】:2015-09-17 21:15:42
【问题描述】:
new_form = f.input :contents, input_html: { class: 'contents-input' }
haml
%p= newsitem.contents

如何完全按照作者使用空格创建的方式呈现newsitem.contents? 例如

我爱苹果

是的,你是

了不起的人。

【问题讨论】:

    标签: css ruby-on-rails input textarea haml


    【解决方案1】:

    您需要使用white-space: pre,它将保留换行符和空格、制表符等:

    p {
        white-space: pre;
    }
    

    当然,你想让p选择器更具体,所以你不要针对所有段落。

    p {white-space: pre; background: #EEE;}
    <p>
    I love apple
    
    Yes You are
    
    awesome person.
    </p>

    【讨论】:

      【解决方案2】:

      您可以使用 Rails 的内置方法 simple_format,它将获取一串文本并使用 HTML 标记对其进行渲染,以赋予其空白格式。

      用法:= simple_format(newsitem.content)

      文档: http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多