1

是否可以使用strip_tags例外,例如:

strip_tags("<b>Bold</b> no more! <div>hellooo</div>", :except => "<strong>")

或更多标签

strip_tags("<b>Bold</b> no more! <div>hellooo</div>", :except => ["<strong>", "<div>"])

我想从字符串中删除除标签之外的所有<strong></strong>标签。

谢谢!

4

2 回答 2

2

直接使用 Rails sanitize 方法(这是 strip_tags 调用的方法)。

http://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html#method-i-sanitize

# Custom Use (only the mentioned tags and attributes are allowed, nothing else)
<%= sanitize @article.body, tags: %w(table tr td), attributes: %w(id class style) %>
于 2013-07-10T18:15:27.587 回答
1

如果您希望它是全局的,请尝试将此代码添加到 config/application.rb

class Application < Rails::Application
  config.action_view.sanitized_allowed_tags = 'table', 'tr', 'td', 'strong', 'div'
end

您的方法调用将保持不变。

如果您希望在每次通话的基础上使用它,请查看以下答案:

Rails 清理删除默认允许的标签

在那里,您将创建一个新的辅助方法来做同样的事情——但所有额外的废话都在辅助方法中,而不是在每次调用中。

于 2013-07-10T18:19:23.793 回答