简体   繁体   English

Rails 3. simple_format 不将结果包装在段落标签中

[英]Rails 3. simple_format do not wrap result in paragraph tags

如何让 simple_format 不将返回值包装在 p 标签中?

simple_format "<span class="required">*</span>"

Unfortunately -- you can't.不幸的是 - 你不能。 If you check out the source at http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format you'll see that the p tags are wrapped around the content unconditionally.如果您在http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format查看源代码,您将看到 p 标签无条件地包裹在内容周围。

You could create a helper that uses the simple_format code, but modify it to not include the p tags...您可以创建一个使用 simple_format 代码的助手,但将其修改为不包含 p 标签...

You can specify wrapper_tag option.您可以指定wrapper_tag选项。

simple_format 'Hello', {}, wrapper_tag: 'span'

This code will be:此代码将是:

<span>Hello</span>

Probably not what you really wanted, but... I ended up doing this:可能不是你真正想要的,但是......我最终这样做了:

module ApplicationHelper
  def nl2br s
    split_paragraphs(sanitize(s, tags: [])).join('<br>').html_safe
  end
end

UPD Or better this: UPD或者更好的是:

def nl2br s
  sanitize(s, tags: []).gsub(/\n/, '<br>').html_safe
end

There's a workaround, if you simply want to get rid of browser-styles:如果您只是想摆脱浏览器样式,则有一种解决方法:

simple_format "<span class="required">*</span>", {}, wrapper_tag: 'p style="margin: 0"'

hackedy-hack-hack ... hackedy-hack-hack ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM