gpt4 book ai didi

ruby-on-rails - Rails : Keep simple_format from adding line breaks to lists?

转载 作者:行者123 更新时间:2023-12-04 03:39:54 27 4
gpt4 key购买 nike

这是输入的HTML:

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

如果我通过Rails的 simple_format传递输出,则输出为:
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</p>

<p><ul>
<br /> <li>Item 1</li>
<br /> <li>Item 2</li>
<br /> <li>Item 3</li>
</ul></p>

<p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

注意每个列表项之前的 <br />

如何获得 simple_format不这样做?

最终,我想做的是在尚未包装在某种标签(例如blockquote,ul,ol)中的内容周围添加段落标签。

最佳答案

据我所知,simple_format旨在仅解决换行符'\n'(当您按Enter键时创建)

simple_format为\n创建一个<br />,为\n\n创建一个<p>
simple_format并非真正设计为与现有的html输入配合使用,但更多用于数据库中的text_area字段和文本字符串等。

有关示例,请参见rails文档:ActionView - simple_format

可以使用content_tag封装整个输入,以提供一些建议:

    <%= content_tag :p, :class => "strong" do -%>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<% end -%>

或者,如果可能的话,格式化您的html而不换行:
  Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

<ul> <li>Item 1 </li> <li>Item 2</li> <li>Item 3</li> </ul>

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

这些都是很强悍的,最好的解决方案是在调用显示之前将所有输入存储为html。但是,不知道如何获得输入,很难提出一种方法来做到这一点。

关于ruby-on-rails - Rails : Keep simple_format from adding line breaks to lists?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4938359/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com