Rails 3. simple_format不会将结果包装在段落标记中
如何使simple_format不将返回的值包装在p标签中?
simple_format "*"
不幸的是 – 你不能。 如果您在http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format查看源代码,您将看到p标签无条件地包含在内容中。
您可以创建一个使用simple_format代码的帮助程序,但将其修改为不包含p标记…
您可以指定wrapper_tag
选项。
simple_format 'Hello', {}, wrapper_tag: 'span'
这段代码将是:
Hello
可能不是你真正想要的,但是……我最终做到了这一点:
module ApplicationHelper def nl2br s split_paragraphs(sanitize(s, tags: [])).join('
').html_safe end end
UPD或更好的这个:
def nl2br s sanitize(s, tags: []).gsub(/\n/, '
').html_safe end
- 如何从rails中的嵌套资源进行路由? 在UsersController#show中获取’ActiveRecord :: RecordNotFound’
- Ruby attr_accessor与getter / setter基准:为什么访问器更快?
- 在Emacs ruby-mode中用制表符缩进而不是空格
- 类的超类不匹配用户 – 从ActiveRecord :: Baseinheritance
- ruby中的打印function
- 用Typhoeus读取可重试的FTP和HTTP URI?
- Geocoder Gem无法在生产环境中工作
- 是否真的需要validation外键?
- 强制所有收到的请求Content-Type为JSON