Ruby-on-rails Rails 3:如何正确显示“textarea”中的文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5852377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:03:22  来源:igfitidea点击:

Rails 3: How to display properly text from "textarea"?

ruby-on-railsruby-on-rails-3textareanewline

提问by Misha Moroshko

In my Rails 3 application I use textareato let users to write a new message in a forum.

在我的 Rails 3 应用程序中,我textarea用来让用户在论坛中撰写新消息。

However, when the message is displayed, all newlines look like spaces (there is no <br />). Maybe there are other mismatch examples, I don't know yet.

但是,当显示消息时,所有换行符看起来都像空格(没有<br />)。也许还有其他不匹配的例子,我还不知道。

I wonder what is the most appropriate way to deal with this.

我想知道处理这个问题的最合适的方法是什么。

I guess that the text that is stored in the database is OK (I see for example that <is converted to &lt;), so the main problem is the presentation.

我猜存储在数据库中的文本是可以的(我看到例如<转换为&lt;),所以主要问题是演示文稿。

Are there build-in helper methods in Rails for this ?

Rails 中是否有内置的辅助方法?

(simple_formatdoes something that looks similar to what I need, but it adds <p>tags which I don't want to appear.)

simple_format做了一些看起来与我需要的相似的事情,但它添加了<p>我不想出现的标签。)

采纳答案by Mischa

Since simple_formatdoes not do what you want, I'd make a simple helper method to convert newlines to <br>s:

由于simple_format没有做你想做的,我会做一个简单的辅助方法来将换行符转换为<br>s:

def nl2br(s)
  s.gsub(/\n/, '<br>')
end

Then in your view you can use it like this:

然后在您看来,您可以像这样使用它:

<%= nl2br(h(@forum_post.message)) %>

回答by Michael Koper

Rails got a helper method out of the box, so you dont have to write your own method.

Rails 有一个开箱即用的辅助方法,因此您不必编写自己的方法。

From the documentation:

从文档:

simple_format(text, html_options={}, options={})

simple_format(text, html_options={}, options={})

my_text = "Here is some basic text...\n...with a line break."

simple_format(my_text)
# => "<p>Here is some basic text...\n<br />...with a line break.</p>"

more_text = "We want to put a paragraph...\n\n...right there."

simple_format(more_text)
# => "<p>We want to put a paragraph...</p>\n\n<p>...right there.</p>"

simple_format("Look ma! A class!", :class => 'description')
# => "<p class='description'>Look ma! A class!</p>"

回答by efatsi

You can use style="white-space: pre-wrap;"in the html tag surrounding the text. This respects any line breaks in the text.

您可以style="white-space: pre-wrap;"在文本周围的 html 标签中使用。这尊重文本中的任何换行符。

回答by Ayonix

If someone still gets redirected here and uses Rails 4: http://apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format

如果有人仍然被重定向到这里并使用 Rails 4:http: //apidock.com/rails/v4.0.2/ActionView/Helpers/TextHelper/simple_format

You can now specify the tag it gets wrapped in (defaults to p) like so:

您现在可以指定它被包裹的标签(默认为 p),如下所示:

simple_format(my_text, {}, wrapper_tag: "div")
# => "<div>Here is some basic text...\n<br />...with a line break.</div>"

回答by mizurnix

CSS-only option

仅 CSS 选项

I believe one of the easiest options is to use css white-space: pre-line;

我相信最简单的选择之一是使用 css white-space: pre-line;

Other answers also mentioned using white-space, but I think it needs a little more information:

其他答案也提到了使用空格,但我认为它需要更多信息:

In most cases you should probably choose pre-lineover pre-wrap. View the difference here.

在大多数情况下,你应该选择pre-linepre-wrap。在此处查看差异。

It's very important to keep in mind about white-spacethat you should not do something like this:

记住white-space你不应该做这样的事情是非常重要的:

<p style="white-space: pre-line;">
  <%= your.text %>
</p>

It will produce extra spaces and line-breaks in the output. Instead, go with this:

它会在输出中产生额外的空格和换行符。相反,请执行以下操作:

<p style="white-space: pre-line;"><%= your.text %></p>

HTML alternative

HTML 替代品

Another way is to wrap your text in <pre>tags. And last note on my CSS option is true here as well:

另一种方法是将文本包装在<pre>标签中。关于我的 CSS 选项的最后一个注释在这里也是正确的:

<p>
  <pre><%= your.text %></pre>
</p>

Don't separate your text from <pre>tags with spaces or line-breaks.

不要将文本与<pre>带有空格或换行符的标签分开。

Final thoughts

最后的想法

After googling this matter a little I have a feeling that html-approach is considered less clean than the css one and we should go css-way. However, html-way seems to be more browser-compatible (supports archaic browsers, but who cares):

在谷歌搜索这个问题之后,我觉得 html-approach 被认为不如 css 干净,我们应该去 css-way。然而,html-way 似乎更兼容浏览器(支持古老的浏览器,但谁在乎):

pre tag

预标签

white-space

空白

回答by BartSabayton

I just used white-space: pre-line. So next line (\n) will render it.

我刚用过white-space: pre-line。所以下一行 (\n) 将呈现它。

回答by cevaris

The following helper preserves new lines as line breaks, and renders any HTML or Script (e.g Javscript) as plain text.

以下帮助程序将新行保留为换行符,并将任何 HTML 或脚本(例如 Javscript)呈现为纯文本。

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

Use as so in views

在视图中如此使用

<%= with_new_lines @object.some_text %>

回答by Jon Gauthier

You'll need to convert the plain text of the textarea to HTML.

您需要将 textarea 的纯文本转换为 HTML。

At the most basic level you could run a string replacement:

在最基本的级别上,您可以运行字符串替换:

 message_content.gsub! /\n/, '<br />'

You could also use a special format like Markdown(Ruby library: BlueCloth) or Textile(Ruby library: RedCloth).

您还可以使用特殊格式,如Markdown(Ruby 库:BlueCloth)或Textile(Ruby 库:RedCloth)。

回答by mansim

I was using Ace code-editor in my rails app and i had problem, that whenever i update or create the code, it adds always extra TAB on every line (except first). I couldn't solve it with gsub or javascript replace.. But it accidently solved itself when i disabled layout for that template.

我在我的 rails 应用程序中使用 Ace 代码编辑器,但我遇到了问题,每当我更新或创建代码时,它总是在每一行(除了第一行)添加额外的 TAB。我无法使用 gsub 或 javascript 替换来解决它。但是当我禁用该模板的布局时,它意外地自行解决了。

So, i solved it with

所以,我解决了

render :layout => false