Html Rails -- 在文本区域中添加换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3137393/
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-08-29 03:38:16  来源:igfitidea点击:

Rails -- Add a line break into a text area

htmlruby-on-railstextarea

提问by ChrisWesAllen

I got a rails app where I can input a few paragraphs of text into my model. The problem is I dont know how to input any line breaks.

我有一个 rails 应用程序,我可以在其中输入几段文本到我的模型中。问题是我不知道如何输入任何换行符。

I've tried to add " {ln}{/ln} ; {&nbsp} and {br}{/br}" but that only displays the html as text and no break.

我尝试添加“ {ln}{/ln} ; { } 和 {br}{/br}”,但这仅将 html 显示为文本并且没有中断。

Is there anyway I can set it so the text area control will use any of the html I place within the model entry?

无论如何我可以设置它以便文本区域控件将使用我放置在模型条目中的任何html?

Is there any thing I can type so rails will recognize, hey put a line here?

有什么我可以输入的东西,所以 rails 会识别,嘿,在这里放一条线?

回答by Karl

Line breaks in textareas are produced as `\n'. However, the problem is that if you simply dump it into your view, it will just be line breaks in your HTML source.

textarea 中的换行符生成为 `\n'。然而,问题是如果你简单地将它转储到你的视图中,它只会是你的 HTML 源代码中的换行符。

You can try using the Rails simple_formathelper to take care of some of this for you: http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M002285

您可以尝试使用 Railssimple_format助手为您处理其中的一些问题:http: //api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M002285

It will auto-convert line breaks to HTML tags. You can use it with something like <%= simple_format(my_text_field) %>.

它将自动将换行符转换为 HTML 标签。您可以将它与类似的东西一起使用<%= simple_format(my_text_field) %>

回答by Ian Lesperance

The problem isn't so much editing the value as it is rendering it later. To add newline characters to your value while editing it in a textarea, just hit the return key. When you re-edit that value later, the whitespace should still be there.

问题不是编辑值,而是稍后渲染它。要在文本区域中编辑值时向您的值添加换行符,只需按回车键。当您稍后重新编辑该值时,空格应该仍然存在。

Rendering the whitespace is the tricky part. In HTML, whitespace is generally insignificant. A renderer like the one your browser uses will display a single space for any continuous string of whitespace. So merely dumping the value onto the page won't be enough:

渲染空白是棘手的部分。在 HTML 中,空格通常是无关紧要的。浏览器使用的渲染器将为任何连续的空白字符串显示一个空格。因此,仅仅将值转储到页面上是不够的:

<%= obj.description %>

Even though your value may be "One \t \n \n Two", it will show up on the screen as "One Two".

即使您的值可能是"One \t \n \n Two",它也会在屏幕上显示为"One Two"

To get those new line characters to actually separate the lines when displayed, you'll need to convert them to HTML before rendering:

要使这些新行字符在显示时实际分隔行,您需要在呈现之前将它们转换为 HTML:

<%= obj.description.gsub(/\n/, '<br/>') %>

Of course, if users are entering data that will be included in your HTML, you should be escaping the values to protect against XSS. If new lines are the only thing you need to support, it should be as simple as this:

当然,如果用户输入的数据将包含在您的 HTML 中,您应该转义这些值以防止XSS。如果新行是您唯一需要支持的内容,那么它应该像这样简单:

<%= h(obj.description).gsub(/\n/, '<br/>') %>

If you want to allow more complex formatting, look into Markdownand Textile(both of which Rails provides helper view methods for). Just be sure to investigate what if any support they provide for XSS prevention.

如果您想允许更复杂的格式,请查看MarkdownTextile(Rails 都提供了辅助视图方法)。只要确保调查他们为 XSS 预防提供的任何支持。

回答by bershika

Keep user input unmodified and add this to your css:

保持用户输入不变并将其添加到您的 css 中:

white-space: pre-line;

It will display \r or \n (enter) in user input as a new line.

它将在用户输入中显示 \r 或 \n(输入)作为新行。

回答by vpsz

Here is another way to display the line breaks in a string while still escaping the rest of the text:

这是在字符串中显示换行符同时仍然转义文本的其余部分的另一种方法:

<%= safe_join(@object.textarea_input.split("\r\n"), "<br />".html_safe) %>

回答by Sagiv Ofek

the problem with simple_formatis that it's also adding other tags like <b><i><hr><h1>...
if you just want line breaks without other tags i suggest you build a partial (lets call it line_break):

问题simple_format在于它还添加了其他标签,例如<b><i><hr><h1>......
如果你只想要没有其他标签的换行符,我建议你构建一个部分(让我们称之为 line_break):

<% text.split("\n").each do |t| %>
  <%= t %><br>
<% end %>

then, just call it from your view:

然后,只需从您的角度调用它:

<%= render partial: 'line_break', locals: {text: some_text} %>

回答by kentor

What version of rails are you using?? Because the way to handle this, is different in rails 2 and 3.

你用的是什么版本的导轨??因为处理这个的方式在轨道 2 和轨道 3 中是不同的。

Let's say the value of the record is "foo<br />bar"

假设记录的价值是 "foo<br />bar"

In rails 3, if you want to evaluate the html, you could do <%=raw "foo<br />bar" %>, if you do so, you'll get a line break when you will see the view.

在 rails 3 中,如果你想评估 html,你可以这样做<%=raw "foo<br />bar" %>,如果你这样做,当你看到视图时你会得到一个换行符。

In rails 2 you don't have to do that, just do <%= "foo<br />bar" %>

在 rails 2 中,您不必这样做,只需执行 <%= "foo<br />bar" %>

Also, HTML doesn't get evaluated in a textarea anyway.

此外,无论如何,HTML 都不会在 textarea 中进行评估。

回答by Yarin

The other answers are wrong. Text area does not render
as line breaks, because the innerHTML value of the TEXTAREA element does not render HTML..

其他答案都是错误的。文本区域不会呈现
为换行符,因为 TEXTAREA 元素的 innerHTML 值不会呈现 HTML。

You need to add the Line feed HTML entity: &#10;

您需要添加换行 HTML 实体: &#10;

EXAMPLE:

例子:

<textarea><%= "LINE 1&#10;LINE 2&#10;LINE 3".html_safe %></textarea>

See also New line in text area - Stack Overflow

另请参阅文本区域中的新行 - VoidCC

回答by Jon Kern

The answers above were good:

上面的答案很好:

  • the gsub(@Ian) worked well
  • the simple_format(@Karl) was a bit over the top as @Aaron pointed out, wrapping everything in <p>
  • gsub(@Ian)运作良好
  • simple_format(@Karl)是一个有点洁癖的@Aaron指出,包皮一切<p>

So I tweaked as follows:

所以我调整如下:

simple_format(value, {}, wrapper_tag: 'div')

回答by mynameiscoffey

\nif memory serves (it hasn't been doing so well today... try at your own risk lol)

\n如果没记错的话(今天表现不佳……请自担风险,哈哈)

Edit: making the assumption you were talking about a textarea, if it is simple output, just use <br>

编辑:假设你在谈论 a textarea,如果它是简单的输出,只需使用<br>