在我的 Rails 3 应用程序中,我使用 textarea
让用户在论坛中撰写新消息。
但是,当显示消息时,所有换行符看起来都像空格(没有 <br />
)。也许还有其他不匹配的例子,我还不知道。
我想知道处理这个问题最合适的方法是什么。
我猜存储在数据库中的文本没问题(例如,我看到 <
被转换为 <
),所以主要问题是演示文稿。
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/