简体   繁体   English

如何从Rails的simple_format()输出中删除空格?

[英]How to remove blanks from Rails' simple_format() output?

I am using Rails' simple_format() to output addresses from my database like this: 我使用Rails的simple_format()从我的数据库输出地址,如下所示:

<%= simple_format(@person.address) %>

As I result I get this markup in my Safari browser: 结果我在Safari浏览器中得到了这个标记:

<p>
  "John Doe "<br>
  "123 Main Street "<br>
  "Eggham "<br>
  "United Kingdom"<br>
</p>

This is fine as long as the text appears left aligned. 只要文本左对齐,这就没问题。

When I want the text to appear right aligned, however, the right edge is not straight. 但是,当我希望文本显示为对齐时,右边缘不是直的。 If you look closely at the HTML, you'll see why. 如果你仔细看看HTML,你会明白为什么。 On all lines there are blanks at the end, except for the last one. 除了最后一行之外,所有行都有空白。

I don't understand where these blanks come from because I always pressed return immediately after entering the last letter of each line. 我不明白这些空白的来源,因为我总是在输入每行的最后一个字母后立即按下return I never entered any of these blanks, yet they appear in every address that simple_format() gives me. 我从未输入任何这些空白,但它们出现在simple_format()给我的每个地址中。

Is there another or better way to print out a textfield in Rails without those annoying blanks at the end? 是否有另一种或更好的方法在Rails中打印出一个文本字段而没有那些烦人的空白?

Thanks for any help. 谢谢你的帮助。

OK, I ended up using this helper method: 好的,我最终使用了这个帮助方法:

def print_multiline(field)
  field.gsub("\r\n","<br/>").html_safe
end

For me this works much better than simple_format() because it won't create whitespace at the end of lines. 对我来说,这比simple_format()更好用,因为它不会在行尾创建空格。

I use it in my views like this: 我在我的观点中使用它,如下所示:

<%= print_multiline(@person.address) %>

input_string.gsub(“\\ r \\ n”,“\\ n”)将用Unix风格的LF字符替换Microsoft风格的CR / LF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM