gpt4 book ai didi

html - rails render helper 返回带有 '\n' 作为新行的 p 标签

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:35 24 4
gpt4 key购买 nike

我得到的这个字符串里面有'\n',我想在有'\n'的地方显示一个新行,而不是显示原始字符串

在 fruits.description 里面

fruits.description: 'This is an red apple \n It is also very sweet'

我的渲染辅助方法

def get_description(fruits)
if fruits.type == 'apple'
haml_tag 'p', fruits.description, {id: fruits.id}
end
end

我想让字符串显示成这样

This is an red apple
It is also very sweet

最佳答案

尝试 simple_format , 这将添加 <p>标记并翻译 \n<br>

具体来说:

Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in <p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is appended.

关于html - rails render helper 返回带有 '\n' 作为新行的 p 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24658760/

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