如何正确地将空格转换为轨道上的ruby中的选项卡?

时间:2015-07-31 14:04:31

标签: html ruby

我正在转换换行符和额外空格。

def simple_format_plus(string)
   (h(string).gsub(/\n/, '<br/>').gsub('  ', "\t")).html_safe
end

在我的观点中,我正在呼唤:

<p><%= simple_format_plus(post.content) %></p>

但是html输出没有显示“\ t”部分。我甚至尝试过禁用css,但问题仍然存在。由于某种原因,我不能连续显示多个空格。

There   are   3   spaces   instead   of   1   in   this   line   .

仍显示为:

There are 3 spaces instead of 1 in this line .

如果我检查<p>元素,则会在开发者控制台中显示额外的空格,但不会显示在浏览器中。

2 个答案:

答案 0 :(得分:1)

标签无法在HTML中进行解释。您可以使用&amp; nbsp三次或多次。辅助方法可以重写为

def simple_format_plus(string)
 (h(string).gsub(/\n/, '<br/>').gsub(' ', "&nbsp&nbsp&nbsp")).html_safe

答案 1 :(得分:0)

连续空格字符在HTML中被解释为单个空格。如果要在源HTML文件中显示每个空格字符,请使用<pre> ... </pre>围绕该部分。