【发布时间】:2015-07-24 14:55:24
【问题描述】:
我有一个文本区域,一些用户可以在其中输入 HTML(请不要对此不安全发表评论,我已经采取了净化对策)。
现在,我想展示一个我的用户可以编写的 html 代码示例。如何使用 ruby 在我的 .erb 模板文件中执行此操作?
最终用户的视觉输出应该是这样的(没有语法高亮,尊重换行符!)
my_example_string:
Here is an example of sample code you can write in the text-area:
<p>Hello, dear Mr. X, new announcement</p>
<br>
<h2>The project</h2>
<p>blablabla</p>
<h2>Your role</h2>
<p>...</p>
EDIT :实际的 HTML 输出看起来像
<p>
Here is an example of sample code you can write in the text-area: !</p>
<p>Hello, dear Mr. X, new announcement;/p>
<h2>The project</h2>
<p>Blablabla</p>
...
</p>
我尝试过类似的东西
<%= 'Here is an example of sample code you can write in the text-area:' +'<br>'.html_safe +
'<br>' + '<br>'.html_safe +
'<p>Hello, dear Mr. X, new announcement</p>'+'<br>'.html_safe +
'<h2>The project'</h2>' + ... %>
但输出看起来像(单行)
Here is an example of sample code you can write in the text-area:<br><br><br><p>Hello, dear Mr. X, new announcement</p><br><h2>The project</h2>
显然我不能混合.html_safe 和非.html_safe 字符串
有什么想法吗?
编辑 2:
我实际上正在使用一个自定义的帮助器/渲染器,它将这个伪工具提示 my_example_string 作为参数
my_view.html.erb
...
<%= render 'shared/field_with_tooltip',
field: f.text_area(:body, placeholder: '', class: 'form-control', value: (@body if @body)),
label: 'Body of the message',
info: my_example_string %>
我的field_with_tooltip 使用这样的信息变量
shared/field_with_tooltip.html.erb
...
<% if info %>
<p class="text-info field-info"><%= info %></p><br>
<% end %>
...
【问题讨论】:
-
您能在页面上发布一个应该是什么样子的示例吗? (纯文本很好。)
-
Emm.. 视觉输出是我在第一个代码块中编写的。你的意思是你想要 HTML 输出?
-
对不起!我正在浏览这个问题并没有意识到!
-
你们有嵌入代码块的能力吗?如果没有,我今天可以为您提供答案。
-
还有一点,视觉输出是一个文本块,对吧?不是 已处理 代码(如显示实际标题)?
标签: html ruby-on-rails ruby erb