Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html Rails:转换文本字段';s行分为<;br/>;_Html_Ruby On Rails_Line Breaks - Fatal编程技术网

Html Rails:转换文本字段';s行分为<;br/>;

Html Rails:转换文本字段';s行分为<;br/>;,html,ruby-on-rails,line-breaks,Html,Ruby On Rails,Line Breaks,我的模型中有一个名为“about”的文本字段,我试图在显示页面上显示该字段,但没有一个换行符正确显示 我尝试了很多方法,但最终得到的代码是: <%= (h @template.about).gsub("\n", "<br />") %> 如何正确地将文本字段的“\n”换行符转换为实际的换行符HTML?我也尝试过简单的格式,但没有用 我使用的是Rails 3,“about”的前几行是: 谢谢你们的鱼,伙计们!不是我想要的,但是。。。UH谢谢 “我来判断,”他说

我的模型中有一个名为“about”的文本字段,我试图在显示页面上显示该字段,但没有一个换行符正确显示

我尝试了很多方法,但最终得到的代码是:

<%= (h @template.about).gsub("\n", "<br />") %>

如何正确地将文本字段的“\n”换行符转换为实际的换行符HTML?我也尝试过简单的格式,但没有用

我使用的是Rails 3,“about”的前几行是:

谢谢你们的鱼,伙计们!不是我想要的,但是。。。UH谢谢
“我来判断,”他说<现在,更多无用的副本,这样我就可以分离出阿曼达发现的那个奇怪的花蕾
等待我是说“奇怪的虫子…”

试一试


h
helper将转义文本,这样您就不会受到XSS攻击。一旦该文本被转义,您就可以
gsub
并将新字符串声明为
html\u-safe


html\u safe
不转义的,它是不应转义字符串的声明。但是您确实希望转义原始字符串,除非您确定它不包含用户输入。

您使用的是哪个rails版本?还提供`
@模板的价值。关于
编辑我的上述评论以反映您的要求…“我也尝试过简单格式”,我想,但谁知道呢。是的,这是我尝试的第一件事。再给它一次机会,只是为了更好的衡量,但它没有发挥作用。奇怪的是,根据我的服务器日志,这是有效的。我把更新后的文本放了一放,结果是:谢谢你们的鱼,伙计们!不是我想要的,但是。。。UH谢谢
“我来判断,”他说!现在,更多无用的副本,这样我就可以分离出阿曼达发现的那个奇怪的花蕾<等等!我的意思是“奇怪的bug…”

然而,在我的展示页面上,它仍然是一堵巨大的文本墙……是它的显示标签,如文本中的
,还是呈现为htmlIt只是不显示标签,既不是文本,也不是HTML
Thanks for the fish, guys! Not like I wanted it, but... uh... thanks? &lt;br /&gt;
<%= simple_format @template.about %>
1.9.3p194 :017 > str = "Thanks for the fish, guys! Not like I wanted it, but... uh... thanks? 
1.9.3p194 :018"> \"I'll be the judge of that,\" he said! 
1.9.3p194 :019"> And now, more useless copy so I can isolate that weird bud that Amanda found. 
1.9.3p194 :020"> Wait! I meant 'weird bug..."
 #=> "Thanks for the fish, guys! Not like I wanted it, but... uh... thanks? \n\"I'll be the judge of that,\" he said! \nAnd now, more useless copy so I can isolate that weird bud that Amanda found. \nWait! I meant 'weird bug..."


1.9.3p194 :021 > simple_format str
 #=> "<p>Thanks for the fish, guys! Not like I wanted it, but... uh... thanks? \n<br />\"I'll be the judge of that,\" he said! \n<br />And now, more useless copy so I can isolate that weird bud that Amanda found. \n<br />Wait! I meant 'weird bug...</p>"
1.9.3p194 :022 > str.gsub("\n", "<br />") 
 #=> "Thanks for the fish, guys! Not like I wanted it, but... uh... thanks? <br />\"I'll be the judge of that,\" he said! <br />And now, more useless copy so I can isolate that weird bud that Amanda found. <br />Wait! I meant 'weird bug..." 
<%= (h @template.about).gsub("\n", "<br />").html_safe %>