ruby-on-rails - rails 3 : How to display properly text from "textarea"?

标签 ruby-on-rails ruby-on-rails-3 textarea newline

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

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

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

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

Rails 中有内置的辅助方法吗?

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

最佳答案

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

来自文档:

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>"

关于ruby-on-rails - rails 3 : How to display properly text from "textarea"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5852377/

相关文章:

ruby-on-rails - "uncaught throw :warden"测试未登录用户的重定向时

ruby-on-rails - Rails 4.2 image_path 具有不同的 Controller url

html - 文本区域溢出 :auto difference in height

html - 我可以在我的表单元素上配置拼写检查属性使用的字典语言吗?

javascript - 如何获取文本区域的旧值

ruby-on-rails - 使用 Carrierwave 调整条件图像大小

ruby-on-rails - 如何为具有 HABTM 关系的模型提供与其他种子模型的关系

javascript - 将其中的一部分替换为js

css - 如何在 rspec (Rails 4/rspec3) 上测试文件大小

javascript - 从 Rails View 调用 JS 函数